Git - Ignore files during merge

GitMergePushIgnoreRepository

Git Problem Overview


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!

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

I ended up finding git attributes. Trying it. Working so far. Did not check all scenarios yet. But it should be the solution.

[Merge Strategies - Git attributes][1]

[1]: https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes#_merge_strategies "Merge Strategies - Git attributes"

Solution 3 - Git

.gitattributes - is a root-level file of your repository that defines the attributes for a subdirectory or subset of files.

You can specify the attribute to tell Git to use different merge strategies for a specific file. Here, we want to preserve the existing config.xml for our branch. We need to set the merge=foo to config.xml in .gitattributes file.

merge=foo tell git to use our(current branch) file, if a merge conflict occurs.

  1. Add a .gitattributes file at the root level of the repository

  2. You can set up an attribute for confix.xml in the .gitattributes file

     <pattern> merge=foo
    

    Let's take an example for config.xml

     config.xml merge=foo
    
  3. And then define a dummy foo merge strategy with:

     $ git config --global merge.foo.driver true
    

If you merge the stag form dev branch, instead of having the merge conflicts with the config.xml file, the stag branch's config.xml preserves at whatever version you originally had.

for more reference: merge_strategies

Solution 4 - Git

You could start by using git merge --no-commit, and then edit the merge however you like i.e. by unstaging config.xml or any other file, then commit. I suspect you'd want to automate it further after that using hooks, but I think it'd be worth going through manually at least once.

Solution 5 - Git

Example:

  1. You have two branches: master, develop
  2. You created file in develop branch and want to ignore it while merging

Code:

git config --global merge.ours.driver true
git checkout master
echo "path/file_to_ignore merge=ours" >> .gitattributes
git merge develop

You can also ignore files with same extension

for example all files with .txt extension:

echo "*.txt merge=ours" >> .gitattributes

Solution 6 - Git

You could use .gitignore to keep the config.xml out of the repository, and then use a post commit hook to upload the appropriate config.xml file to the server.

Solution 7 - Git

As commented by many it should be noted that the accepted answer and the copies of it (using a custom merge strategy for a specific file or files) only works in cases where there would be actual merge conflicts.

2 simple cases that would not result in merge conflicts and therefore commits getting merged:

  • You have a config.xml file in a feature branch and not in master. You want to exclude it during merge
  • Your config.xml file in both branches are based off the same commit. New code is added to the file in feature.

Either use merge --no-commit as demonstrated by unmesh-gurjar or simply cherry-pick invidiual commits into master. The latter being rather cumbersome of course and coming with a couple of other pitfalls (new SHA1 IDs etc.)

Solution 8 - Git

Here git-update-index - Register file contents in the working tree to the index.

git update-index --assume-unchanged <PATH_OF_THE_FILE>

Example:-

> git update-index --assume-unchanged somelocation/pom.xml

Solution 9 - Git

Consider using git hooks:

> After you run a successful git checkout, the post-checkout hook runs; you can use it to set up your working directory properly for your project environment. This may mean moving in large binary files that you don’t want source controlled, auto-generating documentation, or something along those lines.

Source: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks#:~:text=After%20you%20run,along%20those%20lines.

You can put these files in a separate directory that if necessary, can be chosen to exclude from the repository through .gitignore.

Based on the name of the branch that is being switched to, the script from the post-checkout hook can then take the correct files (could be in subdirs) for that branch and copy (overwriting) them to the correct locations. These individual files that are branch-specific, need to be in .gitignore.

Example of post-checkout script in .git/hooks:

#!/bin/sh

BRANCH=`git reflog | awk 'NR==1{ print $8; exit }'`

cp "./configs/$BRANCH/config.json" "./config.json"

exit 0

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKevin RaveView Question on Stackoverflow
Solution 1 - Gitunmesh-gurjarView Answer on Stackoverflow
Solution 2 - GitKevin RaveView Answer on Stackoverflow
Solution 3 - GiteigenharshaView Answer on Stackoverflow
Solution 4 - GitgcbenisonView Answer on Stackoverflow
Solution 5 - GitSole SenseiView Answer on Stackoverflow
Solution 6 - GittlehmanView Answer on Stackoverflow
Solution 7 - GitMattSchmattView Answer on Stackoverflow
Solution 8 - GitSireesh YarlagaddaView Answer on Stackoverflow
Solution 9 - GitPieterView Answer on Stackoverflow