210

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!

3
  • 1
    possible duplicate of Git: ignore some files during a merge (keep some files restricted to one branch) Commented Feb 12, 2014 at 21:24
  • 3
    did you ever find a solution to this? git attributes are only useful for the case where the file has conflicts between the branches being merged, so its not always enough. Commented Aug 23, 2016 at 6:00
  • did you look into symbolic (not followed by git) or even hard links to the rescue? Commented Nov 1, 2017 at 8:10

9 Answers 9

196

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.

Sign up to request clarification or add additional context in comments.

7 Comments

I'd add "--no-ff" if you want a file not be overwritten when merge happens in fast-forward strategy.
so what does git reset HEAD myfile.txt do exactly? How does it help with our goal here?
How do you put lines 2 and 3 in a loop?
@AndroidDev You can do something like this: for i in `ls myfile.txt myfile_2.txt`; do echo $i; git reset HEAD $i && git checkout -- $i; done
@Kid_Learning_C : git reset HEAD myfile.txt unstages the file (which was staged post --no-commit merging)
|
78

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

6 Comments

Here is the correct link for the merge strategies section of the git attributes docs: git-scm.com/book/en/v2/…
Found this well explained in an article medium.com/@porteneuve/…
This seems to be a problem if there are no conflicts? Files will still be merged?
@Brett, In fact, you can keep this file always conflicting on every branch, so you can avoid automatic merging because of there is no conflict. For example, add one line to the file of every branch: <text>{branch_name}</text>
|
38

.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

7 Comments

But what if there is no conflict? How to always keep a certain file during merge?
@IgorYalovoy: If there is no conflict ... well, this is a little tricky as it actually works from hash IDs, but: if config.xml is completely unchanged from the merge base to either branch tip version, Git just takes the changed version, without looking at the merge.ours.driver setting. So you are correct to be concerned.
copy-paste from Git Docs: Merge Strategies - Git attributes. Link
"ours" appears to be an arbitrary name for the new strategy being introduced. I found this confusing, because theirs is a built-in merge strategy. But it seems that one can write <pattern> merge=foo, then git config --global merge.foo.driver true, and it will work the same way.
Adding a .gitattributes file to the main branch with <filename-to-exclude> merge=ours, then running git config --global merge.ours.driver true worked great. The config file in dev was not merged into the config file in main. Unfortunately, during the merge the .gitattributes file was deleted (because it did not exist in dev)! How do avoid this? Just add the same .gitattributes file to dev? Would the ours value cause a conflict?
|
14

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 

1 Comment

This is working only in situation where have a conflict!
9

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.

1 Comment

Does git attributes offer any straight solution? I am not able to understand this. git-scm.com/book/en/…
8

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.)

3 Comments

What is the resolution to this?
@Gabriel: The sad truth is there is no set-and-forget solution as simple as adding a file to a .gitignore afaia. You'd need to use one of the workarounds above (which they are imho) or unfortunately work with other VCSs.
You could also consider the accepted answer to stackoverflow.com/questions/14369378/… (shows a variation using merge --no-commit)
5

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.

3 Comments

I know thats kind of hack. I am trying to find a clean solution. What about git attributes. Any idea how it works? I am not able to understand this. git-scm.com/book/en/…
I have not heard of these, I'm trying to apply the merge=ours attribute, looks promising.
Git attributes are awesome! You can even tell git how to diff non-text files, so you could use pdf2text to diff PDFs!
3

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

2 Comments

That's nice when pushing but still complains when pulling might overwrite the file.
have you tried to add your file entry in .gitignore file. @Rolf
1

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.