Git - Ignore files during merge

Kevin Rave picture Kevin Rave · Mar 5, 2013 · Viewed 108.5k times · Source

I have a repo called myrepo on the remote beanstalk server.

I cloned it to my local machine. Created two additional branches: staging and dev. Pushed these branches to remote as well.

Now:

 local                   remote                   server
 --------------------------------------------------------  
 master  ==> Pushes to  `master`  ==> deployed to `prod`
 staging ==> Pushes to  `staging` ==> deployed to `staging`
 dev     ==> Pushes to  `dev`     ==> deployed to `dev`

I have a file called config.xml which is different on each branch.

I want to ignore this file only during merges. But I want this to be included when I checkout or commit from / to the repo branch.

The reason I want this is, we have a deploy script that pulls (checkout) the specific branch and deploys on the respective servers. So we need config.xml file of that specific branch go into the specific server as indicated above when deployed.

I guess .gitignore wont work. What are the other options? Note that the ignored file should be part of checkout and commit, which is important. it should be ignored only during merges.

Thanks!

Answer

unmesh-gurjar picture unmesh-gurjar · May 9, 2013

I got over this issue by using git merge command with the --no-commit option and then explicitly removed the staged file and ignore the changes to the file. E.g.: say I want to ignore any changes to myfile.txt I proceed as follows:

git merge --no-ff --no-commit <merge-branch>
git reset HEAD myfile.txt
git checkout -- myfile.txt
git commit -m "merged <merge-branch>"

You can put statements 2 & 3 in a for loop, if you have a list of files to skip.