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

GitGitHubOpen SourceSoftware & Coding

Let us say we performed a merge 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 either −

  • reset command; OR
  • revert command

The git reset command can be used to undo local changes to the state of a Git repository. A git reset moves the HEAD pointer to a given commit and updates the index to match that commit. This command rewrites the commit history. However, if we have shared our commits with other team members or if we have pushed our code to the remote repository this method should not be used.

Consider the following example, where the repository has two branches – master and a feature branch. Both the branches are diverged and have two commits each. We need to merge the commits (C3 and C4) into the master branch. Let us assume that project compilation fails due to a faulty merge. This is shown in the diagram below.

Let us solve this issue by using the ‘git reset –hard’ command to reset the HEAD pointer to the previous commit in master (c2). This is shown in the diagram below. The command for resetting the HEAD pointer will be

git reset --hard HEAD~1 //moves one step back

The faulty merge will be garbage collected as there is no reference to the faulty merge.

$ 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
$ git reset --hard HEAD~1
HEAD is now at 862ce37 mh
$ git log --oneline --all --graph
* 862ce37 (HEAD -> master) mh.txt
| * e1c99eb (feature) lmno
|/
* 91773ff abc
raja
Published on 28-Apr-2021 14:15:25
Advertisements