Back to: Git Course
So you made a commit… and you immediately regret it. Maybe the message was embarrassing, you forgot to add a file, or you just went down the wrong path entirely. Don’t worry – fixing this is easy, as long as you haven’t pushed it anywhere yet.
Let me show you the two main ways to delete or undo a local commit: git reset (the heavy hammer) and git commit --amend (the quick fix). We’ll also talk about git revert for when you already pushed (spoiler: that doesn’t delete commits, but it’s good to know).
Before we start: figure out where you are
Run this to see your recent commits:
git log --oneline
You’ll see something like:
a1b2c3d (HEAD -> main) Oops, wrong file e4f5g6h Actually working feature i7j8k9l Initial commit
Your latest commit is at the top. That’s the one we’ll mess with.
Case 1: You just made the commit and want to completely delete it (lose the changes)
Use git reset with --hard – but be careful, this throws away the changes from that commit entirely.
git reset --hard HEAD~1
That means: “move the current branch back by one commit, and delete everything from the commit I’m removing”. After this, git log will no longer show that commit.
✅ Use this when: you made a commit by mistake and don’t care about the changes inside it.
⚠️ Warning: any uncommitted changes you had will also be gone. If you want to keep them, see the next case.
Case 2: You want to delete the commit but keep the changes as “unstaged” (so you can redo it)
This is my personal favorite. You keep the file edits but remove the commit itself.
git reset --soft HEAD~1
Now run git status. You’ll see all the files from that commit sitting there, unstaged, ready for you to add again or modify.
✅ Use this when: you forgot to include a file, you want to split a commit into smaller ones, or you just want to rewrite history cleanly.
Case 3: You want to delete a commit that’s not the most recent one
Say your commit history looks like this:
abc123 Bad commit (the one you want gone) def456 Good commit ghi789 Another good commit
You can’t just delete the middle one without affecting the ones after it. But you can use git rebase -i to rewrite history.
git rebase -i HEAD~3
That opens an editor with a list of the last 3 commits. Find the line with the bad commit and change the word pick to drop (or just delete that line entirely). Save and close.
Git will replay the remaining commits, skipping the bad one. Your good commits stay intact.
✅ Use this when: you need to remove an older commit from your local branch before anyone else sees it.
⚠️ Only do this if you haven’t pushed those commits anywhere. Rebasing shared history causes pain for teammates.
Case 4: You already pushed the commit to a remote (GitHub, GitLab, etc.)
Do not use git reset on a branch that others might be using. Instead, use git revert. It doesn’t delete the commit – it creates a new commit that undoes the changes.
git revert HEAD
Now push as usual. Your history will show both the original mistake and the revert commit, but the code will be back to how it was before.
✅ Use this for public/shared branches. It’s safe and keeps everyone happy.
Quick reference cheat sheet
| Situation | Command |
|---|---|
| Delete last commit, toss changes | git reset --hard HEAD~1 |
| Delete last commit, keep changes | git reset --soft HEAD~1 |
| Delete an older commit (local only) | git rebase -i HEAD~n then drop |
| Undo a pushed commit safely | git revert HEAD then push |
The most common “oops” – fix the commit message or add a missing file
If you just committed and only want to change the message or add one more file, don’t delete anything. Use --amend:
git add forgotten-file.txt git commit --amend -m "New, better message"
This replaces the last commit with a new one. No deletion needed, and it’s super clean.
Final advice
-
If you’re not sure, make a backup branch first:
git branch backup. That way you can always come back. -
git reset --hardis like a delete key – it’s permanent. Double-check withgit statusbefore running it. -
When in doubt, use
--soft. You can always discard changes later manually. -
If you already pushed, do not reset. Use
revertor ask your team.
You’ve got this. Git is forgiving when you’re working locally – worst case, you clone again. Now go delete that embarrassing commit with confidence. 😄
