Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git rebase failure with auto-generated file

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.

like image 603
Component 10 Avatar asked Sep 28 '22 20:09

Component 10


2 Answers

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.

like image 82
Alexander Groß Avatar answered Oct 06 '22 08:10

Alexander Groß


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?

like image 1
skelliam Avatar answered Oct 06 '22 08:10

skelliam