How to undo the commit in git
Home » BLOG » Web development » How to undo the commit in git

How to undo the commit in git

category:  Web development

Today I have a situation where I fetch the changes from the master branch(remote repo) to my dev local branch. After trying to merge the changes, I have the merge conflict warning. The warning said something like “git can not perform automatically merge”. However, I accidentally commit the merge conflict files without fixing the conflict. And I also push the changes to my dev remote branch. OOP!

Because I forget to fix the merge conflict. I have to undo the merge commit before other developers will pull my changes.

What I want to do, I want to reset the pointer of git to point to the commit before the merge commit. After resetting, I have to fetch the changes from the master branch(remote repo) to my dev local branch again. Then I have to fix the merge conflict before committing the changes on my local branch. Finally, I have to push the changes from my dev local branch to my dev remote branch. Below is how I did.

Git reset

Here is the command to undo the commit. This affects my local branch.

Note: Before using –hard option, you need to make sure you don’t want the previous commits before < commit-hash-to-go-back >. Because they will be deleted from your local branch.

$ git reset --hard <commit-hash-to-go-back>

The commit hash is for the commit you want the pointer point back. I will pick the last commit before the merge commit. To find the commit hash(40 characters), you can use the command below.

$ git rev-parse 3cdd5d

3cdd5d is a short hash you see on the commit lists.

Or you can find the commit hash by clicking on each commit and grabbing the commit hash at the URL after commit slug (/commit/6311ad8c57458b4833a7738d5gb9fa298829ca3f).

After you run `git reset`, you should see the info similar to below.

$ git reset --hard xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
HEAD is now at xxxxxxx commit-message

x is the commit hash.

After running the `git reset`, in your local git, the git pointer is already reset to the commit-hash-to-go-back your input. If you don’t need to fix the merge conflict just like in my situation, you can push the changes to the remote repo. So you can go to “Push to remote branch“.

Fetch and merge the changs to my local branch

After resetting, I will fetch and merge the changes from the master remote branch to my dev local branch one more time. Below are the commands.

git fetch origin
git merge origin/master

I see the merge conflict again.

Fixing the merge conflict and commit it

Now I have the merge conflict just like I had before. This time, I fix the merge conflict and commit to my dev local branch. Below is the commit command.

git add <merge-conflict-files>
git commit -m "your-comment"

In VSCode, it will show the merge conflict for you. So you will find and fix them easily.

Push to remote branch

Finally, I push the changes from my dev local branch to my dev remote branch with the command below.

git push origin dev --force

With –force option, the previous commit before the specific commit hash I pick in “git reset”, will be deleted from the dev remote branch. I can check the commits by viewing the commits list page from GitHub or bitbucket.

Now, my dev local branch and dev remote branch are clean.