Home > Software design >  git merge commit removes files, but `git show SHA` doesn't show them being removed
git merge commit removes files, but `git show SHA` doesn't show them being removed

Time:03-16

I have this situation in git:

-F--M--F1-->
   /
  /
-D--D1-->
  • F is a commit on a feature branch, which contains the file in question.
  • D is a commit on the main development branch, which does not contain the file.
  • M is a merge commit that merges the development branch into the feature branch.

If I checkout F, I see the file. If I checkout M, the file is gone.

But git show --name-status M does NOT show the file being removed. It instead shows two other files with 'MM' (literally) in the status column, meaning both sides had changes in the merge.

What is happening? How is the file removed in commit M without git showing it as removed?

CodePudding user response:

git show on a merge commit is very special. Only those files are shown that are different from both parents. In your case, the deleted file is "identical" to one of the parents (i.e., it is deleted in one parent, D, and deleted in the result M), so it is not shown.

There is a mode that can help:

git show -m M

This computes a diff to each parent and shows them as a separate patch (or whatever display mode you choose).

CodePudding user response:

You probably merged the main branch into the feature branch, not the other way around
So the file is only visible to F

  •  Tags:  
  • git
  • Related