How to undo a faulty merge with revert command in Git?

GitGitHubOpen SourceSoftware & Coding

Let us say we performed a merge commit, shared this commit and later found out that the code is not compiling or our application is not working. This happens if we make mistakes while merging. In situations like these we need to undo the merge using the git revert command.

Consider the following diagram, where the repository has two branches – master and a feature branch. Both the branches are diverged and have two commits each. The commits in the feature branch the (C3 and C4) are merged into the master branch and the resulting commit is shared. Let us assume that project compilation fails due to a faulty merge.

We will not use the git reset command to revert the faulty merge as the faulty merge is shared and we do not want to rewrite the history. Unlike the reset command, the git revert command will invert changes introduced by the faulty merge commit and create a commit which will replicate a desired commit.

The syntax for using the revert command is

$git revert -m <parent> HEAD

The -m flag in the above command is mandatory and should be followed by a number greater zero. This is because, the merge commit has two parents - one parent on the master branch and other on the feature branch. To revert the merge commit we should tell git how we want it to revert the changes. The -m option is used to specify the parent commit that the new commit should replicate.

In our example, the faulty merge has two parents; c2 of the master branch is the first parent and C4 of the feature branch is the second parent. The following command will undo changes made by the faulty merge and create a new commit that will replicate the first parent – c2.

$git revert -m 1 HEAD

This is shown in the diagram below

This scenario is illustrated below-

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo
$ git init
Initialized empty Git repository in E:/tut_repo/.git/

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ echo abc>abc.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git add .

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git commit -m 'abc'
[master (root-commit) 91773ff] abc
1 file changed, 1 insertion(+)
create mode 100644 abc.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git branch feature

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git switch feature
Switched to branch 'feature'

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ echo lmno>lmno.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git add .

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git commit -m 'lmno'
[feature e1c99eb] lmno
1 file changed, 1 insertion(+)
create mode 100644 lmno.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git switch master
Switched to branch 'master'

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git log --oneline --all --graph
* e1c99eb (feature) lmno
* 91773ff (HEAD -> master) abc

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ echo mh>mh.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git add .

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git commit -m 'mh.txt'
[master 862ce37] mh.txt
1 file changed, 1 insertion(+)
create mode 100644 mh.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git log --oneline --all --graph
* 862ce37 (HEAD -> master) mh.txt
| * e1c99eb (feature) lmno
|/
* 91773ff abc

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git merge feature
hint: Waiting for your editor to close the file... unix2dos: converting file E:/tut_repo/.git/MERGE_MSG to DOS format...
dos2unix: converting file E:/tut_repo/.git/MERGE_MSG to Unix format...
Merge made by the 'recursive' strategy.
lmno.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 lmno.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git log --oneline --all --graph
* 8216241 (HEAD -> master) Merge branch 'feature'
|\
| * e1c99eb (feature) lmno
* | 862ce37 mh.txt
|/
* 91773ff abc

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git revert HEAD
error: commit 821624117bbdaab5cc581ef9a29e22ed40bf3679 is a merge but no -m option was given.
fatal: revert failed

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git revert -m HEAD
error: option `mainline' expects a number greater than zero

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ ls
abc.txt lmno.txt mh.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git revert -m 1 HEAD
Removing lmno.txt
hint: Waiting for your editor to close the file... unix2dos: converting file E:/tut_repo/.git/COMMIT_EDITMSG to DOS format...
dos2unix: converting file E:/tut_repo/.git/COMMIT_EDITMSG to Unix format...
[master 12ae6f3] Revert "Merge branch 'feature'"
1 file changed, 1 deletion(-)
delete mode 100644 lmno.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ ls
abc.txt mh.txt
raja
Published on 28-Apr-2021 14:17:50
Advertisements