Thursday, July 28, 2016

Merging git branches while keeping some files from the old branch

Lets say we have master and experimental branch.
Master has evolved and we would like to merge its changes in the experimental branch, that has not yet lost its value.

But there are some files in the experimental branch we would like to keep.

How can we do that?

First we need to create our own merge driver. A merge driver is the command that is responsible for the actual merging of the files. In this case we need a driver that does not do anything to the files we want to preserve.

We can accomplish that by using this command:

git config --global merge.ours.driver true


This driver will always return true for every file that meets a specific pattern. What is this pattern and how do we configure it?

We need to create a file called .gitattributes in our project, version it, and add as contents the pattern of the files we would like to keep from being merged:

echo 'email.json merge=ours' >> .gitattributes
git add .gitattributes
git commit -m 'chore: Preserve email.json during merges'

This will protect a file called email.json from being merged.

After that we execute
git merge -

to complete the merge.

If you need more details please check this link: https://medium.com/@porteneuve/how-to-make-git-preserve-specific-files-while-merging-18c92343826b#.fbw1wyyby

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.