Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Paramanantham Harrison
Paramanantham Harrison

Posted on • Originally published atlearnwithparam.com on

     

How to revert back to older commit in git

I have my new FREE workshop onAPI design for beginners using Node Js and MongoDB. It is my first workshop, let's learn together. You can register for the workshophere

If we mess up something and know that it was working before two or three commits. We often want to go to that previous commit and check it out.

we will see how to do that using git commands,

There are three possible scenarios,

  • Just go to the previous commit and then come back to the latest commit (temporary jump)
  • Go back to the previous commit and modify some code there but don’t want to lose the current update history too
  • Go back to the previous commit and discard all the new updates after that.

Just go to the previous commit and then come back to the latest

This is probably the easiest one. The steps to follow are,

  • git stash to stash any uncommitted changes
  • git log to check the commit hash for the previous commit you are looking for

then

git checkout <commit-hash>
Enter fullscreen modeExit fullscreen mode

This will automatically go to the commit and show the stale branch. Once you finish what you want to look for in the code, you can once again go back to the latest by running

  • git checkout master or any other branch name if you are in a different branch
  • If you did stash any uncommitted changes, then rungit stash pop to pop the uncommitted changes from the stash array

Go back to the previous commit and modify, but keep the latest commits somewhere

This is a typical situation where you don’t want to lose your new commits but want to check and fix something in the old commit.

You can do that same thing until get the commit hash for the previous commit and then,

Check out a new branch from the previous commit,

git checkout-b branch-name <commit-hash>
Enter fullscreen modeExit fullscreen mode

In this way, you can keep both the latest commit and separate branch to debug and fix from the old commit.

Go back to the previous commit and discard all the latest commit after that

Again it is simple to do,

git reset--hard <commit-hash>
Enter fullscreen modeExit fullscreen mode

This simply reset to the old commit point and discard all new commits. One disadvantage with this approach is, you lose the history of all the latest commit. Potentially you might lose some relevant code.

This command also won’t work as expected if you have uncommitted changes, so stash it before running this.

There is one more way to achieve this usinggit revert. If you know the exact number of commits you want to go back in history, then you can do so by,

git revert HEAD~3# here 3 is the number of the commit you want to go before to the latest commit
Enter fullscreen modeExit fullscreen mode

One advantage with revert is, it keeps the history and create a new commit to revert all the changes. You can even create your own message by not committing automatically when doing revert.

You can also revert selective commits using commit hashes.

  • git log and find all the commits you want to revert. Copy those commit hashes
git revert hash1 hash2#... and so on
Enter fullscreen modeExit fullscreen mode

In this way, you can selectively revert some of the commits and keep the code with proper commit log and history.

I hope you learned some tricks about git. Stay tuned for more git magics and JavaScript tutorials 🎉

Follow me ontwitter. I share my reading list and short tips on twitter

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I help beginners to become a pro software engineers through backendchallenges.com
  • Location
    Tallinn, Estonia
  • Work
    Head of Engineering at Jobbatical
  • Joined

More fromParamanantham Harrison

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp