Book Your Free Call

How To Uncommit Your Changes In Git?

Tim Mouskhelichvili
Tim Mouskhelichvili
3 minutes to read

While working on a git repository, committing is the most common action that a developer will do. It is so common that sometimes developers will make mistakes. This brings the question of how to uncommit your changes in git? You can undo your changes in git by using the git reset command.

To uncommit your last commit but KEEP the previous changes use:

bashgit reset --soft HEAD^

To uncommit your last commit, and THROW AWAY the previous changes use:

bashgit reset --hard HEAD^

In this article, I will explain, in detail, how to uncommit your changes, as well as answer some of the most common questions.

Let’s get to it 😎.

git uncommit

How to uncommit the last commit?

To undo the last commit you can use one of those three methods depending on your needs.

1. When you want to keep the changes.

If you want to uncommit the last commit and keep the previous changes unstaged use:

bashgit reset HEAD^

HEAD refers to your current commit. HEAD^ refers to your last commit.

To verify that the command worked use the git status command.

2. When you want to keep the changes in the staging area.

If you want to uncommit the last commit and keep the previous changes in the staging area use:

bashgit reset --soft HEAD^

3. When you want to throw away the changes.

If you want to uncommit the last commit and throw away the changes use:

bashgit reset --hard HEAD^

Newly created files that were never added to the index will need to be deleted separately, using the git clean command.

If you have already pushed the changes to a remote, you will need to override those changes using the git push -f command.

git uncommit specific commit

How to undo a specific commit?

To undo a specific commit you first need to find the commit's hash that you want to uncommit. You can do it by using the git reflog or git log commands. Once you have the commit's hash, use the git reset command as in the section above.

Here is how to uncommit a specific commit but keep the changes in the staging area:

bashgit reset --soft COMMIT_HASH

Here is how to uncommit a specific commit, and throw away the previous changes:

bashgit reset --hard COMMIT_HASH

How to remove a committed file after a push?

If you want to remove a committed file after a push, follow those steps:

  1. Use git checkout HEAD^ -- /path/to/file to remove the file.
  2. Use git commit -am 'revert' to commit the changes.
  3. Finally, use git push to push the changes.

Final thoughts

Manipulating the commit history is a very important skill to have. Every developer must learn this, whether you are a front-end or a back-end developer.

And, now you too know how to uncommit your changes in git.

git uncommit

I have written more tutorials on git if you are interested in learning more about it and its commands.

If you have any questions please ask them in the comments section. I always check for new comments and respond to them.

Thank you for reading through this article.

Please share it with your fellow coders and colleagues.

Comments (0)