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 :
8cc7371
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
?
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.
simply run git reset --hard to revert all those changes.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With