Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a revert commit also be a merge commit?

Recently at work, we lost some code on a project under version control using git (and a private github repository).

I used git bisect to find the culprit commit, and I got really surprised discovering it was looking like a revert commit, but also had two parents like merge commits :

commit b5f97311eea2905baf8a96fddec22321d952f05c
Merge: 8cc7371 131d463
Author: Bob <[email protected]>
Date:   Fri May 22 19:42:25 2015 +0200

    Revert "ISB-CPW-3627 - Mise en place du calage des filtres DS1/DS2/DS3/DS4"

    This reverts commit 8cc7371e7133b68db95ac8e9aeb730d7889a1166.

This issue has been resolved by creating a new branch from an ancestor commit, and applying some patches on top of that new branch.

So I don't need help to fix that, I just want to understand how can a revert commit could also be a merge commit, what happened, and how to reproduce this weird behavior.

I tried to reproduce it like this :

  • create a new branch at 8cc7371
  • git revert 8cc7371

It creates a revert commit with a single parent (8cc7371) as expected.

The culprit commit author uses Atlassian SourceTree instead of the native command line interface, so I also tried using the revert feature within SourceTree and got the same result as above (single parent).

After running some git commands on b5f9731, I noticed that git diff and git show are not reporting the same file set :

$ git diff b5f9731..8cc7371 --name-only
=> 2 files :
   application/sites/frontend/views/scripts/Layout/Foobar/PointsDeVente/index.tpl
   public/media/design/frontend/css/main-dev.css
$ git show --oneline --name-only  b5f9731
=> 1 file :
   public/media/design/frontend/css/main-dev.css

SourceTree and Github are showing 2 files.

You can find how the graph looks like here : graph.html, and some git commands results there : misc.html.

Could anybody explain what happened with b5f9731 ?

like image 878
Vince Avatar asked Jun 13 '15 14:06

Vince


People also ask

Is it possible to revert a merge commit?

You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog . git reflog is a better option because things are more readable with it.

How do I revert a merge commit that has already been pushed to remote?

simply run git reset --hard to revert all those changes.

How do you check if a commit is a merge commit?

git show --summary HEAD | grep -q ^Merge: This will return 0 for merge commits, 1 for non-merge commits. Replace HEAD by your desired commit to test.


1 Answers

What comes to my mind:

git revert ...
# new commit abcdef
git merge ...
# new commit fedcba
git rebase -i HEAD~2

pick abcdef - the original revert commit
squash fedcba - the merge commit

Message is taken from abcdef.

This all results in the new b5f9731. It has two parents from a merge commit and the message from the revert commit.

like image 169
Nick Volynkin Avatar answered Oct 06 '22 01:10

Nick Volynkin