How to revert a git commit from a remote repository? Easy step-by-step tutorial.

Sharath Ravi
3 min readMar 21, 2020

--

In this post, I will share my knowledge on how to undo a commit which was pushed to a remote repository. I am writing this post as I had come across a similar situation where undoing the commit was critical and I couldn’t find a proper article dedicated to this.

Without further adieu, jumping straight to the steps:

  1. git log --oneline (to get the commit hash that you wish to revert)
  2. git checkout <commit-hash>
  3. git revert <commit-hash> (this will give you a new commit hash with “Revert” word in the beginning of the message)
  4. git branch <new-branch-name> <new-commit-hash>
  5. git push origin <new-branch-name>

Now, lets dive deep into the steps.

The first step.

The first step would of course be to get the hash of the particular commit that you wish to undo. To get the hash, you should run git log which would yield you the below output:

To get the get log in one line, you can run the command ‘git log — oneline’ as below:

Find the commit you want to revert from the list of commits. If you’re unable to find your commit, continue to press enter to find your commit. In my case, the commit I want to revert would be ‘3dfebd1b’.

The second step.

After you get the particular commit hash, run the command ‘git checkout’ as below:

This would make the current working repository match the state of this exact commit that you wish to undo.

The third step.

Then, run ‘git revert 3dfebd1b’. This would yield the output below:

This will open up the file in your default editor with the respective commit content:

You will have to verify the content in the editor and close the file opened. This would give the following output in the command prompt.

This command will create a new commit with the “Revert” word in the beginning of the message, as you can see above. Copy the new commit hash. In my case, it would be ‘9002f5c4’ as you can see above.
After this, if you check your repository status(using ‘git status’), you’ll notice that you have the HEAD detached at the commit you tested before.

The fourth and the final step.

Then, push the new commit hash to a new branch in local (I created a local branch called ‘test-revert’) and push the branch to remote using commands:

git branch test-revert 9002f5c4

git push origin test-revert

That’s all! Bingo! You’ve got a new branch with the reverted commit changes.

I hope this helped at least some of you and made your day. Please let me know in the comments below if you know better ways of doing this. Thank you!

--

--

Sharath Ravi

Founder & CEO at Offline Human Studios. Wingman at M^2. Passionately Observing AI Evolution