How to revert or undo last commit in Git?

Revert last commit

Get the last Commit ID

// It gives the details of the previous commits to the branch$ git log

Revert last commit

$ git revert COMMIT_ID

Undo last commit

$ git reset --hard COMMIT_ID
$ git reset — hard HEAD~1
  • When you use the — hard flag to undo a merge, any uncommitted change will be reverted. It means you lost your local uncommitted changes.
  • If you use — soft flag to undo a merge, it keeps your uncommited changes in the local directory.

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Siddharth Rastogi

Siddharth Rastogi

I am a full stack developer, I have an expertise in Web Development. I write tech stuff and share my knowledge with others with the help of articles.