Git commit undo/revert
Tags: git, github
Published: 2020-09-24 15:12:55

# Yea We make mistakes, this is called being Human
Fix a wrong git commit <br><br>

##### 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.
<br><br><br>
_In this post I'll try to show how we can undo a git commit, using command line._
<br><br>
_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?_
<br><br><br>

** 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? <br>
* git log
or
* git log --oneline
<br><br>

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

```commit < last commit hash>
   Author: Tufail Shah <example@email.com>
   Date:   Sun Sep 22 00:00:00 2020 +0000
   {my commit message}
... ```
<br><br><br>
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}
<br><br>

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'
<br><br>
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' 
<br/><br><br>
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 ```
<br><br>

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}<br>

<br><br><br><br>
You can also visit Atlassian for detailed info on git [Atlassian Git](https://www.atlassian.com/git/tutorials/undoing-changes)
## Summary

If you want to test the previous commit just do 
* git checkout 'test commit hash' <br><br>
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'  <br><br>
To fix the detached head do 
* git checkout 'current branch'.<br>

<br><br>
You can also visit Atlassian for detailed info on git [Atlassian Git undoing changes](https://www.atlassian.com/git/tutorials/undoing-changes)

<br><br><br>
###Look me up on:
> Twitter: @tufailmsg
> GitHub: https://github.com/ShahTufail

`Git` `revert` `svn` `coding`