How to Git uncommit the last commit
Let it be stated up front and before we go any further that there is no git uncommit command.
There is a git commit command. There is a git reset command. There is a git clean command. And there is a largely misunderstood git revert command. But there is no such thing as an uncommit command in Git.
Having said that, the desire to uncommit in Git is fully understandable. You’ve been doing some work since you last did a Git pull or merged a branch, you don’t like the changes you’ve made, and you want to take yourself back to the way your workspace was when the last Git commit happened. That’s not an unreasonable request.
Git uncommit command
To perform a git uncommit, the first command you’ll want to run is a hard reset
[email protected] /c/repo/reset (main-branch) $ git reset --hard
A hard reset will delete any new files that were added to the index, and undo any updates or changes made to any files that were part of the repository when the last commit occurred. This command typically fulfills the need for a git uncommit command.
It’s worth noting that any newly created files that were never added to the index will remain on the filesystem. You can leave them alone if you want to keep them around, or you can delete them manually. Or, you can delete them with the following git command:
[email protected] /c/repo/reset
$ git clean -fxd
How to uncommit Git files
To recap, the steps to perform a git uncommit are:
- Open a command prompt or terminal window in the root of your Git repository
- Run a git reset –hard command to undo all tracked changes
- Manually delete any new files created since the last commit that were not tracked
- A git clean -fxd command can achieve this aim as well
And that’s how easy it is to do a Git uncommit.