In this post, we’ll cover how to uncommit in Git. If you mistakenly executed a
git commit but haven’t pushed it to the remote repository yet, you can undo the last commit without losing your changes or data.
Although there’s no specific
git uncommit or
git undo command, Git provides other commands like
git clean, and
git revert to manage workflows.
The need for uncommitting in Git is genuine, such as when you incorrectly commit files using conventional or semantic commit that should have been committed in a different context.
⚠️ Important Note: The git revert command is often misunderstood. It’s crucial to recognize that this command does not actually “undo” a commit. Instead, it creates a new commit that undoes the changes made in the previous commit, preserving the commit history.
Undoing a commit can be risky if you’re not familiar with the process, but it’s straightforward if you understand how it works. Here are four different ways to undo a
Let’s assume the following context, where
C is your HEAD, and
(F) represents the state of your files:
git reset --hard to undo the commit and completely remove all changes. In our scenario, commit
C would be destroyed, and any uncommitted changes would be discarded.
git reset --hard HEAD~
git reset --hard HEAD~ now
B is the HEAD. Because of the use of
--hard, the files are reset to their state at commit
If you did
git reset --hard but need to get that destroyed code back there’s still a way to get it. Run this command:
git reflog will show you a list of (partial) commit
shas (that is, hashes) that you’ve moved around in. Then you can find the commit you destroyed, and run:
git checkout -b someNewBranchName shaYouDestroyed
You’ve now recovered that commit.
This can be done because commits don’t actually get destroyed in Git for some 90 days, so you can usually go back and rescue one you didn’t mean to get rid of.
git reset HEAD~, you can undo the commit and unstage all files.
This option is suitable if commit
C wasn’t entirely wrong but required some adjustments. You can undo the commit, keep your changes, edit them, and then make a better commit.
Starting again with
C as the HEAD commit:
To undo the commit and unstage files, just leave off the
git reset HEAD~1
Now the the result would be:
Note that in both cases HEAD is just a pointer to the latest commit.
When you do a
git reset HEAD~1, you tell Git to move the HEAD pointer back one commit. But (unless you use –hard) you leave your files as they were. So now git status shows the changes you had checked into
C. You haven’t lost any changes!
git reset --soft HEAD~ you can undo commit and keep all files staged.
This is the lightest touch, you can even undo your commit but leave your files and your index:
git reset --soft HEAD~1
This not only leaves your files alone, it even leaves your index alone. When you do
git status, you’ll see that the same files are in the index as before. In fact, right after this command, you could do git commit and you’d be redoing the same commit you just had.
Beware that this actions might not do what you expect if your erroneous commit was a (fast-forward) merge! If your head is on a merge commit (ex: merged branch feature into master),
git reset --hard~1will point the master branch to the last commit inside the feature branch. In this case the specific commit ID should be used instead of the relative command.
If the commit was previously pushed to the remote repository, any undo operation will cause some problems to the rest of the users who have this commit in their local copy, when they do a
git pullin the future. So, if the commit was already pushed, run this instead:
git revert <bad-commit-sha1-id>
git push origin
Even without a
git undo command, what would be really trick to implement and use, Git offer options to back commit changes with the opportunity to redo mistakes and organize correctly your source code.