165

I'm rebasing in git, and one conflict I get is 'both added' - that is, exactly the same filename has been added independently in my branch, and in the branch I'm rebasing on. git status tells me:

# Unmerged paths:
#   (use "git reset HEAD <file>..." to unstage)
#   (use "git add/rm <file>..." as appropriate to mark resolution)
#
#       both added:         src/MyFile.cs

My question is, how do I resolve this? Must I use a merge tool or is there a way I can do it just from the commandline? If I git rm src/MyFile.cs, how does git know which file version I want to remove and which I want to keep?

Jez
  • 27,951
  • 32
  • 136
  • 233

3 Answers3

172

If you use git rm git will remove all versions of that path from the index so your resolve action will leave you without either version.

You can use git checkout --ours src/MyFile.cs to choose the version from the branch onto which you are rebasing or git checkout --theirs src/MyFile.cs to choose the version from the branch which you are rebasing.

If you want a blend you need to use a merge tool or edit it manually.

ekuusela
  • 5,034
  • 1
  • 25
  • 43
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • 1
    Thanks. And I just realized that the reason the merge tool wasn't working was because git creates the .LOCAL and .REMOTE files for the merge, but not the .BASE file. I think it should just create an empty .BASE file. If you manually create the empty .BASE file, the merge tool works fine. – Jez Mar 22 '12 at 14:29
  • 1
    @Jez: Please see this thread: http://thread.gmane.org/gmane.comp.version-control.git/188776/focus=188867 – CB Bailey Mar 22 '12 at 14:57
  • 1
    So are you saying this will be fixed in a recent version of git? – Jez Mar 22 '12 at 16:01
  • 1
    @Jez: It's in git versions >= 1.7.9.1 http://git.kernel.org/?p=git/git.git;a=commit;h=ec245ba04944473ba6657a826643ef90ed02b51d – CB Bailey Mar 22 '12 at 16:29
  • 34
    From @Tom answers : When doing ... `git checkout --ours someFile` **It may seem like it didn't do anything when doing git status.** Just Remember to do this afterwards. `git add someFile` `git status` – pec Oct 26 '15 at 01:27
  • "the branch onto which you are rebasing" and "the branch which you are rebasing" are really vague. Can't we just assume the common use-case, and use something like "local" and "remote", or "current" and "new"? – aroth Jan 11 '21 at 03:31
84

I sometimes find it confusing using the --theirs and --ours options to identify where the file will come from. Most of the time mine will be in the branch I am rebasing which is referred to by --theirs!

You can also use git checkout <tree-ish> -- src/MyFile.cs

Where the <tree-ish> can be replaced either by the branch name or commit-id that contains the file you wish to keep.

git checkout 6a363d8 -- src/MyFile.cs

git checkout my_branch -- src/MyFile.cs

git checkout HEAD -- src/MyFile.cs

Anas Alkhatib
  • 1,110
  • 1
  • 9
  • 22
  • I can not stress how precise and important is this to be understood. Ours and theirs is just hiding what you really want, and that is to point which file you want to use as a resolution when both are added. – zhrist Sep 21 '21 at 23:13
52

When doing ...

git checkout --ours someFile

It may seem like it didn't do anything when doing git status.

Just Remember to do this afterwards.

git add someFile
git status
Tom
  • 1,610
  • 1
  • 15
  • 19