2

When merging or rebasing two branches in git, and a file has been deleted in one but modified in the other, git gives a conflict. git status shows the file as "deleted by us" or "deleted by them".

I've found plenty of questions talking about how to resolve the conflict, including:

However, most of the answers assume you already know exactly what the conflict is, and just need to tell git to keep or delete the file.

How do you find out what changed in the version of the file that was not deleted? git simply leaves the whole file in the working copy, and doesn't mark any changes.

For instance:

  • on branch develop, user A changes file foo.x
  • on branch feature, user B refactors the code, deletes file foo.x because its functionality is now in other files
  • user B tries to merge develop into feature, and gets a conflict marked deleted by us
  • how does user B see what user A changed, to see if the changes are needed in the refactored code?
IMSoP
  • 89,526
  • 13
  • 117
  • 169

1 Answers1

2

If "deleted by us":

git diff <your-branch>...<branch-you're-merging-to> /path/to/file

If "deleted by them":

git diff <branch-you're-merging-to>...<your-branch> /path/to/file

From the git-diff man page:

git diff [<options>] <commit>...<commit> [--] [<path>...]

This form is to view the changes on the branch containing and up to the second <commit>, starting at a common ancestor of both <commit>. "git diff A...B" is equivalent to "git diff $(git merge-base A B) B". You can omit any one of <commit>, which has the same effect as using HEAD instead.

This will only show you the changes made in the branch that didn't delete the file i.e. if the branch that deleted the file made any changes to it first those won't be reflected in the diff.

If you're rebasing rather than merging then the "deleted by us" / "deleted by them" messages will be the other way round so use the other command.

Calum Halpin
  • 1,945
  • 1
  • 10
  • 20
  • Is there a way to determine the two commits, without manually going through the logs and finding their commit hashes? – IMSoP Jul 19 '19 at 09:59
  • You can use `merge-base ` to find the commit before your work and just use the name of the branch you're merging to for the second. – Calum Halpin Jul 19 '19 at 10:13
  • I've updated the answer with a more convenient form. – Calum Halpin Jul 19 '19 at 10:18
  • Thanks! Does this work for both "deleted by us" and "deleted by them"? And can the same approach be used when rebasing? (Sorry if that sounds picky - I'm trying to make sure this is a good reference for future readers.) – IMSoP Jul 19 '19 at 10:19
  • I've updated it to cover both "updated by us" and "updated by them". I think it should work the same way when rebasing but I haven't checked. – Calum Halpin Jul 19 '19 at 10:26