1

Context: I am trying to get git behave like some other source code management tool while trying to convince them to only use git. In the meantime I need to run git as a slave, doing all the same things as the scm in action.

How can I force git to use the strategy -X theirs on file level instead of hunk level?

Basically if I have two hunks from two branches in the same file, git is capable to merge those to include both. But instead of that I want to overwrite the whole file of mybranch with the file of theirbranch.

EDIT:

Same can be done the other way round, to merge with strategy -X ours on file level and overwrite changes on theirbranch if there are changes on the same file on mybranch.

inetphantom
  • 2,498
  • 4
  • 38
  • 61
  • manually `git checkout -- `? – iBug Jan 14 '19 at 13:11
  • Looks like you can even force the --theirs strategy at merge level : https://stackoverflow.com/questions/10697463/resolve-git-merge-conflicts-in-favor-of-their-changes-during-a-pull – Matt Jan 14 '19 at 13:15
  • Sure. Wait a few minutes for me to compose the answer. – iBug Jan 14 '19 at 13:32
  • @Matt recursive merge with strategy theirs will only take theirs when a conflict occurs. If I change the 1st line of a file in one branch and the 20th in the other branch git will merge that without errors and not applying `theirs` option – inetphantom Jan 14 '19 at 13:52

1 Answers1

0

According to Resolve Git merge conflicts in favor of their changes during a pull, if you want to perform a merge with their changes, you can use git merge -X theirs. This, however, cannot be applied on a file basis.

If you want to discard "our" changes and use "their" version of the file, you can just perform a checkout:

git checkout <their revision or branch> -- <path to file>

Or, as given in this answer, there's also a --theirs option when in the conflict state:

git checkout --theirs <path>

Just don't forget to git add and git commit after resolving conflicts.


If you want a list of files with differences in two branches (two arbitrary commits):

git diff <branch 1> <branch 2> --name-only
iBug
  • 35,554
  • 7
  • 89
  • 134
  • This is how I will do it. Just instead of doing a `git checkout` I will be overwriting the files from the other scm. – inetphantom Jan 14 '19 at 14:05