More explanation for given answer git revert -m 1 <merge commit id>
with a graphical representation and an example, step-by-step.
Reverting a merge commit is not straightforward as with git revert <commit-hash>
, since Git gets confused when looking back from the merge commit due to its two parent commits. To specify the desired parent, uses the -m
flag. As git cannot determine which parent is the mainline and which is the branch to un-merge automatically, so this must be specified.
The iss53
branch was merged into master
, creating a Merge Commit
, C6
. C6
had two parents, C5
and C4
.
Need to revert C6
and return the repository to its state at C4
. So it must specify which parent to use for the revert command.
For that check the git log
, (here representing actual commit hash with code names from graph)
> git log
commit C6
Merge: C4 C5
Author: Mozz <[email protected]>
Date: Wed Feb 29 23:59:59 2020 +0100
Merge branch 'iss53' to master
...
From git log
output, note down the parent IDs that come with Merge: - -
. It will be in the format of Merge: parent1 parent2
, here Merge: C4 C5
.
The C4
commit is in master
branch and we need to revert to that, that is parent 1 and -m 1
is needed here (use git log C4
to verify the previous commits to confirm parent branch).
Switch to the branch on which the merge was made ( it is the master
branch here and we aim to remove the iss53
branch from it )
Do the git revert with -m 1
flag.
# To revert to C4 in master branch
git revert C6 -m 1
# C6 - is the merge commit hash
For some other cases, if needed revert to C5
,
# revert to C5 in iss53 branch
git revert C6 -m 2
# General
git revert <merge commit id> -m 1 (reverts to parent1)
git revert <merge commit id> -m 2 (reverts to parent2)
# remember to check and verify the parent1 and parent2 with git log command.
Practical example
Created a new branch revert-test
on an existing project that has only main
branch, The commit graph looks like this now.
(For graphical view of commits use —graph
with git log
[SO ans ref] OR this more interactive VS code extension - git graph)
Now, I've added some new files, modified existing files, and created separate commits on each branch, then pushed them to the origin. The graph now looks like this:
Then, created a pull request from GitHub and merged revert-test
branch to main
.
I want to undo the merge commit and go back to the last commit in the main
branch - which is 12a7327
Note that the merge commit - 2ec06d9
has two parents now - 12a7327
(in main
) and 15bde47
(in revert-test
), checking git log
now,
> git log
commit 2ec06d9d315a3f7919ffe4ad2c2d7cec8c8f9aa3 (HEAD -> main, origin/main, origin/HEAD)
Merge: 12a7327 15bde47
Author: Akshay <[email protected]>
Date: Sun Feb 5 00:41:13 2023 +0530
Merge pull request #1 from Akshay/revert-test
Revert test
To revert the merge commit and get back to 12a7327
need to do,
# To the First parent
git revert 2ec06d9 -m 1
Now a commit message will show in editor that specifies the details, check and verify.
So that creates a Revert commit that does the inverse changes of the merge commit.
Lastly push the changes, Now the merge commit changes gone and the Log will look like,