Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't git diff show anything for unmerged paths

Tags:

git

merge

When merging two branches, many times the merge fails, and I have to resolve the diffs individually.

Some files merge successfully, and I can see the diff:

git diff --staged <merged-file>

The problem is that for unmerged paths, when I try to check the diffs introduced, I see nothing:

git diff <unmerged-file>

But when I try to diff it against the remote master branch, then the diffs appear:

git diff origin/master <unmerged-file>

Why is this happening? Is the merge operation updating my local HEAD or something?
Moreover, when i use git add to mark resolution, they never go into the staging area -- is that the reason why git diff is not showing anything?

like image 982
Kartik Anand Avatar asked Mar 06 '14 05:03

Kartik Anand


1 Answers

During a merge, the conflicting files are in a special state. There exists multiple entries with the same filename and different blob id's. These are usually three blobs (used for the three way merge) or two blobs (for a simple merge).

Attempt the merge..

$ git merge origin/otherbranch
Merge remote-tracking branch 'origin/otherbranch' into mybranch

Conflicts:
    somefile.txt

Review the files which need merging

$ git diff --name-status --diff-filter=U
U       somefile.txt

or

$ git update-index --refresh
somefile.txt: needs merge

Review the blobs which relate to the file being merged;

$ git ls-files -s somefile.txt
100644 9a0579524e0c7ba9fc9ae18badadaddcad2d598f **1** somefile.txt
100644 1bcff16b6de5ed304a06e643070e40787db1ead8 **2** somefile.txt
100644 6e52271b22f6a6d1150619433551e0fa9094b108 **3** somefile.txt

According to the git-merge man-page. 1 = common ancenstor, 2 = HEAD (ours) and 3 = MERGE_HEAD (theirs)

Show differences

$ git diff 9a0579524e0c7ba9fc9ae18badadaddcad2d598f 6e52271b22f6a6d1150619433551e0fa9094b108
< some differences >

Retrieve the common ancestor..

$ git cat-file blob 9a0579524e0c7ba9fc9ae18badadaddcad2d598f

Checkout version from HEAD

$ git checkout --ours somefile.txt

checkout version from MERGE_HEAD

$ git checkout --theirs somefile.txt

Reset to 'merged' changed

$ git checkout -m somefile.txt
like image 195
Dave Avatar answered Sep 30 '22 07:09

Dave