6

I'm working on a code base where one file is autogenerated and changes very frequently. I find that rebasing my local branch usually results in falling back to three way merges and failure on this file. I then have to fix the file up and continue, but this can happen a number of times in one rebase and, due to the nature of the file, manual conflict resolution is usually pretty nasty. In fact, the whole process ends up in a nasty mess and a complete waste of time since there is no value added in attempting to merge this particular file.

Given that it is recreated as part of the build process anyway, is there a way I can tell Git to ignore this file during the rebase?

Please note that I've looked at other similar questions / answers but did not see anything that specifically addressed this (though please correct me if there's something out there.) Also, for the record I'm not keen on this type of file being in version control anyway, but I'm afraid I have no choice in the matter.

Component 10
  • 10,247
  • 7
  • 47
  • 64
  • Why do you have no choice in it being in version control? – cmbuckley Feb 03 '15 at 11:37
  • Essentially, it is is part of my (very large) clients normal process and there are technical and historic reasons that this is the case that are too long winded to go into here. Naturally, I have raised my objections, but I am not in a position to make this decision. – Component 10 Feb 03 '15 at 11:43

4 Answers4

2

If the file is generated by your build, it should not be under version control in the first place.

git rm --cached <path/to/file>
echo <path/to/file> >> .gitignore
git add .gitignore
git commit -m "Removed <path/to/file> from version control"
Alexander Groß
  • 10,200
  • 1
  • 30
  • 33
1

Despite my earlier answer that I still think is the cleanest option, you can still have git help you a bit with your always-conflicted file given your special situation that you cannot influence what files are under source control.

You need to tell git that it should always resolve conflicts for a specific file given a specific strategy (here: theirs win).

git init

# Define a no-op merge driver for this repo
git config merge.theirs.driver true
# The file always-conflict is solved using the just setup 'theirs' driver
echo always-conflict merge=theirs >> .gitattributes

# Create two conflicts:
# - always-conflict is resolved automatically
# - other-conflict needs to be resolved by you
echo master-1 > always-conflict
echo master-1 > other-conflict
git add --all
git commit -m master-1

echo master-2 > always-conflict
echo master-2 > other-conflict
git add --all
git commit -m master-2

git checkout -b feature HEAD~1
echo feature > always-conflict
echo feature > other-conflict
git add --all
git commit -m feature

git rebase master

# The rebase will stop, but it'll only have you solve other-conflict.

If you don't want to have the .gitattributes file committed to version control, use .git/info/attributes instead.

Alexander Groß
  • 10,200
  • 1
  • 30
  • 33
1

You could tell git to treat that specific file as binary by adding myfile.name binary to the .gitattributes file, where myfile.name is the name of the file causing you problems. This would tell git that the file is a binary type and should not be merged.

See: How do I make Git treat a file as binary?

skelliam
  • 522
  • 3
  • 11
0

You can put the file (the filename(s) and/or path) into a file called .gitignore This will prevent git from recognizing the file at all.

frlan
  • 6,950
  • 3
  • 31
  • 72