13 November 2026 · One command a day
How do I undo the last git commit without rewriting history?
git revert HEAD undoes the last commit by adding a new commit that does the exact opposite. The bad change goes from your files but the history stays as it was. Nothing is lost, and anyone who has already pulled is not left with a broken copy. Use it when a commit you just made, or just deployed, turns out to be wrong.
I learn my automation tips in the AI Profit Boardroom. Have a look here. Affiliate link.
The command
cd /var/www/appname
git revert HEAD
git revert works out the reverse of a commit and saves it as a new commit. HEAD means the commit you are on now, which is usually the last one you made. Git opens your text editor with a ready-made message such as Revert "Update upload limit". Save and close it to finish. Add --no-edit to accept that message without the editor, which helps in scripts. To undo an older commit, put its short hash in place of HEAD.
What you will see
[main 7c1e4b2] Revert "Change upload limit for example.com"
1 file changed, 1 insertion(+), 1 deletion(-)
The first line shows the branch, the hash of the new commit and its message. The second line counts what changed. The line the original commit added is removed, and the line it removed comes back. If you then list recent commits one line each, you will see the original commit with the revert directly above it.
When to use it
- You committed and pushed a config change, it has broken the site, and you need the old version back quickly.
- Another person or a deploy script has already pulled the commit. Rewriting history with
git resetwould cause conflicts for them. - You want a clear record that a change was tried and then backed out.
Watch out for
A revert only changes the files in the repository. It does not change what is already running. If your app loads its code when it starts, or your site needs a build step, the live site stays the same until you rebuild or restart it, for example with pm2 restart appname. If you reverted an nginx file, run sudo nginx -t and then sudo systemctl reload nginx. Many people revert, see the same error, and think the revert failed, when the old process is just still running.
Questions people ask
Does git revert delete the original commit?
No. The original commit stays in the history and a new commit cancels it out. That is why it is safe on a branch you have already pushed.
How do I undo a revert?
Revert the revert. Run git revert HEAD again straight after it, and the original change comes back as another new commit.
Rolling back a live config in seconds is the kind of thing you can only do on a server you control. You can get the same VPS with 20% off at https://asksteves.co.uk/vps. Affiliate link.
Want more automation tips? The AI Profit Boardroom on Skool is where I get mine. Have a look here. Affiliate link.
Everything on this blog runs on one Hostinger KVM 8. This link takes 20% off the same box.
Affiliate link.