DEV Community

Cover image for Git commit undo/revert
Tufail Shah
Tufail Shah

Posted on

Git commit undo/revert

Yea We make mistakes, this is called being Human

Fix a wrong git commit

While starting, Git seemed very punishing, unapologetic, not afterthoughts, YOGO kind. But now as I started to get to know git a bit better and I understood that under this facade, somewhere deep-down there is a soft corner.

Enough of the drama, let's get to the point.




In this post I'll try to show how we can undo a git commit, using command line.



Suppose we made a wrong commit or extra files in commit or something which is not complete or development ready or simply something not meant to be pushed yet.
Now i want to go back. What to do? Should i run to the forests?




** Don't Panic **
First let's check what went wrong and what are the other commits which i need to consider before giving a PR.
How do you see the last commit?

  • git log or
  • git log --oneline

It will give us the list of commits made by you and other people working on the same repository.
Like this:

commit < last commit hash>
Author: Tufail Shah <example@email.com>
Date: Sun Sep 22 00:00:00 2020 +0000
{my commit message}
...





To test a specific commit (e.g.: {last commit hash}), that you think has the last working version, you can type the following:

  • git checkout {commit hash}

This will make the working repository match the state of this exact commit.

After you do this you’ll get the following output:

  • root@debian:/home/debian/test-project# git checkout 'commit hash' Note: checking out 'commit hash'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout.

After analyzing the specific commit, if you then decide to stay in that commit state, you can undo the last commit.

If you wish to undo/revert the last commit you can do the following, using the commit hash that you get from the git log command:

  • git revert 'commit hash' This command will create a new commit with the “Revert” word in the beginning of the message. After this, you’ll notice that you have the HEAD detached at the commit you tested before.

root@debian:/home/debian/test-project# git status
HEAD detached at 45uik34f3

To fix this and attach back the HEAD to your working repository, you should checkout the branch you are working on:
git checkout {current branch}






You can also visit Atlassian for detailed info on git Atlassian Git

Summary

If you want to test the previous commit just do

  • git checkout 'test commit hash' then you can test that last working version of your project. If you want to revert the last commit just do
  • git revert 'unwanted commit hash' To fix the detached head do
  • git checkout 'current branch'.




You can also visit Atlassian for detailed info on git Atlassian Git undoing changes

Look me up on:

Twitter: @tufailmsg
GitHub: https://github.com/ShahTufail

Git revert svn coding

Top comments (0)