How to delete or undo unpushed Git commits
Codemia Knowledge Hub ·
Use git reset --hard HEAD~N to delete the last N unpushed commits and discard their changes, or git reset --soft HEAD~N to undo the commits while keeping the changes staged for a new commit. Since the commits have not been pushed, rewriting local history affects nobody else.
Checking which commits are unpushed
An unpushed commit exists in your local repository but has not been sent to the remote (GitHub, GitLab and so on). To list them:
Removing commits with git reset
git reset moves the branch pointer backward, removing commits from the branch history. The mode decides what happens to the file changes in those commits.
Soft reset: keep changes staged
Your working directory is unchanged and everything from the removed commits is staged, so you can make one new commit from it:
This is a handy way to squash several small commits into one before pushing.
Mixed reset (the default): keep changes unstaged
The changes stay in your working directory but are not staged, so you can review them, stage selectively and recommit:
Hard reset: discard everything
This is destructive. The commits are removed and their file changes are deleted from your working directory. Use it when you want to abandon that work completely.
Undoing commits with git revert
git revert creates new commits that reverse earlier ones instead of rewriting history:
It is mainly meant for pushed commits, but it works on unpushed ones too. Unlike git reset, the original commits stay in the history.
Resetting to a specific commit hash
Instead of counting back with HEAD~N, reset to a commit hash:
Every commit after c3b2a1d is removed, however many there are.
Removing a commit from the middle with interactive rebase
To drop specific commits while keeping the ones around them, use interactive rebase:
The editor shows:
Delete the line for the commit you want to remove (or change pick to drop), then save and close the editor. Git replays the remaining commits without it.
Comparison of methods
| Method | Changes to files | History | Best for |
git reset --soft HEAD~N | Kept (staged) | Commits removed | Squashing commits before push |
git reset --mixed HEAD~N | Kept (unstaged) | Commits removed | Re-organizing changes into new commits |
git reset --hard HEAD~N | Discarded | Commits removed | Completely abandoning work |
git revert HEAD | Reversed by new commit | Preserved + new commit | Undoing pushed commits (or audit trail) |
git rebase -i HEAD~N | Depends on action | Rewritten | Removing specific commits from middle |
Recovering from an accidental hard reset
If a git reset --hard threw away work you need, the reflog still has it:
Reflog entries are kept for about 90 days by default.
Making the branch match the remote exactly
To throw away all local commits and make your branch identical to the remote:
This discards all unpushed commits and any uncommitted changes.
Mistakes to avoid
Resetting pushed commits. Running git reset --hard and then git push --force on a shared branch rewrites public history and breaks the repository for everyone who pulled those commits. Use git revert for pushed commits.
Forgetting that plain git reset means --mixed. git reset HEAD~2 keeps your changes but unstages them. Always pass the flag explicitly.
Miscounting HEAD~N. HEAD~1 removes only the latest commit; HEAD~2 removes the last two. Check with git log --oneline before resetting.
Expecting untracked files to disappear. git reset --hard does not delete files that were never added to Git. git clean -fd removes them, but that deletion is permanent and cannot be recovered through the reflog.
Rebase conflicts. If later commits depend on a commit you drop in git rebase -i, Git stops with a merge conflict. Resolve it and run git rebase --continue.
Related reading
- How do I diff the same file between two different commits on the same branch?
- How do I discard unstaged changes in Git?
- How do I do a 'git status' so it doesn't display untracked files without using .gitignore?
- How do I exit from the text window in Git?
- How do I exit the results of 'git diff' in Git Bash on windows?
- How do I fetch all Git branches?
- How do I fetch only one branch of a remote Git repository?
- How do I find and restore a deleted file in a Git repository?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.