Committing changes is a regular phenomenon when you work with Git. The traditional workflow of Git is such that you build your project in modules or pieces, add them up to the staging area, and commit them in the working tree. After committing, your code becomes ready to push on the remote repository.
However, if you have ever been in a situation where after committing changes or files, you realize that you just made a mistake that you don’t want to push, or you have forgotten to add a file, then this guide is for you to walk through the mechanism of uncommitting changes in Git.
Table of Contents
Case#01: Undo Last Commit and Keep Previous Changes
$ git reset HEAD^
Case#02: When you Want to Keep the Uncommitting Data in the Staging Area
$ git reset --soft HEAD^
Case#03: When you Want to Uncommit both the Committed Data and the Data in the Staging Area
$ git reset --hard HEAD^
Case#04: If you Want to Go Back by Two Commits
$ git reset HEAD~2
Case#05: When you Want to Undo a Specific Commit
$ git reset [commit_hash]
If you are a Windows user, keep the hash commit number or HEAD within double-quotes. For example:
$ git reset “HEAD^” $ git reset “f145h7r”