Created by Esko LuontolaLinked to 2.3m issues across 261 teams
In order to undo the most recent local commits in Git, you can use the git reset
command. This command will undo your last commit while leaving your working tree (the state of your files on disk) untouched. After running the git reset
command, you will need to make any necessary corrections to the working tree files. Once you have made the necessary corrections, you can use the git add
command to add the changes to the new commit. Finally, you can use the git commit -c ORIG_HEAD
command to commit the changes, reusing the old commit message.
git commit -m "Something terribly misguided" # (0: Your accident) git reset HEAD~ # (1) [ edit files as necessary ] # (2) git add . # (3) git commit -c ORIG_HEAD # (4)
Alternatively, you can use the git commit --amend
command to edit the previous commit (or just its commit message).
If you need to remove (not revert) a commit that has been pushed to the server, you will need to rewrite history with the git push origin main --force[-with-lease]
command. It is almost always a bad idea to use --force
, so it is recommended to use --force-with-lease
instead.