Search code examples
gitmergegit-merge

Merge did not introduce merged changes


I had a branch which was merged off of commit A of branch main. I made a few changes and ended up like this:

A-B-C-D

I then merged the branch back to main introducing a merge commit. A fast-forward merge would have sufficed but our policy is to create a merge commit. I noticed a spelling mistake in the merge's commit message and did a git commit --amend to correct it. The history now looks like this. Where E is the merge commit.

A-B-C-D-E

Parents of E are displayed by git log as Merge: A D.

The problem now is that main does no longer contain the changes of commits B, C or D.

  • For example C changed file foo/bar but when opening the file as of E the file is in state `A´.
  • Also a git log -- foo/bar does not list commit C.
  • Executing git diff A E shows an empty diff as does git show E.

This is the corresponding section from my reflog:

E HEAD@{93}: commit (amend): Finish Hotfix
Z HEAD@{94}: commit (merge): Finsh Hotfix
A HEAD@{95}: checkout: moving from hotfix to main
D HEAD@{96}: commit: Changes
C HEAD@{97}: commit: Changes
B HEAD@{98}: commit: Changes
A HEAD@{99}: checkout: moving from main to hotfix

When I try to merge the changes again (git merge D) git states Already up to date..

  • How did I end up like this? Caused amending the problem?
  • What is my best option to fix this issue without cluttering the history too much? The branches have been pushed already a few days ago and cannot be rewritten anymore.

Solution

  • I am still not sure what caused the problem in the first place but it seems I made a mistake during the merge.

    However, I was confused by the fact that git show E does not show any changes. This is caused by a misconception of mine of how git show works. How it works is explained in detail in this answer. So executing git show -m E shows how the merge commit reverts all the changes of B, C and D.
    After understanding this I think that a revert commit it the most elegant solution to fix the problem. That is executing git revert -m2 E.