Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolving a 'both added' merge conflict in git?

Tags:

git

merge

rebase

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?

like image 636
Jez Avatar asked Oct 12 '22 14:10

Jez


People also ask

How do you resolve Add add conflict?

You get CONFLICT (add/add): Merge conflict in ... Or, an easier way to see the difference, right after the failed git merge when you are in the conflicted state, you can simply run git diff without parameters, it will reveal the conflicted regions. Sometimes the right content is a mix of the two files.


2 Answers

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.

like image 160
CB Bailey Avatar answered Oct 14 '22 03:10

CB Bailey


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

like image 82
Anas Alkhatib Avatar answered Oct 14 '22 02:10

Anas Alkhatib