Always use the local version of the file when git merge (even if there are no changes on one side of the merge)

This question is very similar to: How do I tell git to always select my local version for conflicting merges on a specific file? with a significant difference.

I followed the excellent steps in VonC's answer to the above question and it almost works. However, in my situation, I only have changes to the input version of a file (not both the local version and the input version).

No merge required as only one branch changes. In this case it turns out that git doesn't try to start the merge driver and just overwrites the local version with the incoming one, which is exactly what my custom merge driver should have prevented.

Is there a way to essentially tell git to never modify a particular file during a merge (even in the circumstances above where VonC's answer doesn't seem to work quite well)?

+3


source to share


1 answer


It looks like others have tried and failed to force a conflict when Git decides it's not there.

This SO question Git merge - manual merge - forcing conflicting with the full version of the old and new file "concludes in the comments:

What I am most afraid of is the answer: "this is not possible, since this works with Git".

And " Need a Git workflow suggestion " mentions try:

*.xml merge=Unset

to the file .gitattributes

.
Or, a custom merge driver is ~/.gitconfig

trying to cause automatic merge to fail

In other words, it might not be the right approach.

If you are sure that all the changes made by the merge should not be here, you can try:

git clone yourRepo aCloneRepo
cd aCloneRepo
git merge --no-commit --no-ff origin/source_branch
# simply overwrite any change file by the ones in ..\yourRepo
git add .
git commit -m "merge"

      



Note that the final " git merge

" will work even if yours git status

mentions that there is nothing to do (since you are restoring the contents of all modified files).
It will write the merge to a new commit.

This will give you:

C:\Users\VonC\prog\git\tests\mergekeep\r4>git lg
*   a6a1588 - (HEAD, master) merge (2 minutes ago) <VonC>
|\
| * 1bcd75d - (origin/b) addition (2 hours ago) <VonC>
|/
* 9d2e8fb - (origin/master, origin/HEAD) first file (2 hours ago) <VonC>

      

But with local files unchanged.

And if you try to merge your branch again, you get:

C:\Users\VonC\prog\git\tests\mergekeep\r4>git merge origin/b
Already up-to-date.

      

However, this workaround is cumbersome, involves a clone and some manual copy or rsynch from one working tree to another.
I've tried playing around with various Git reset commands or commands like Git update-index , but without much success.

+2


source







All Articles