Git
Version Control
Coding
Programming Tips
Commit Deletion

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:

bash
1# Show commits that exist locally but not on the remote
2git log origin/main..HEAD --oneline
3
4# Or check the status summary
5git status
6# "Your branch is ahead of 'origin/main' by 3 commits."

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

bash
# Undo the last 3 commits, keep all changes in the staging area
git reset --soft HEAD~3

Your working directory is unchanged and everything from the removed commits is staged, so you can make one new commit from it:

bash
git commit -m "Combined changes from previous three commits"

This is a handy way to squash several small commits into one before pushing.

Mixed reset (the default): keep changes unstaged

bash
# Undo the last 2 commits, keep changes in working directory but unstaged
git reset HEAD~2
# Same as: git reset --mixed HEAD~2

The changes stay in your working directory but are not staged, so you can review them, stage selectively and recommit:

bash
git diff                  # Review what changed
git add specific-file.py  # Stage only what you want
git commit -m "Cleaned up implementation"

Hard reset: discard everything

bash
# Undo the last 2 commits AND throw away all changes
git reset --hard HEAD~2

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:

bash
1# Revert the most recent commit
2git revert HEAD
3
4# Revert a specific commit by hash
5git revert a1b2c3d
6
7# Revert multiple commits (oldest to newest order)
8git revert HEAD~3..HEAD

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:

bash
1# Find the commit you want to go back to
2git log --oneline
3# Output:
4# f4d5e6a Fix typo in README
5# c3b2a1d Add user authentication     <-- keep everything up to here
6# b2a1c3d Initial project setup
7
8# Reset to that specific commit
9git reset --hard c3b2a1d

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:

bash
# Rebase the last 5 commits interactively
git rebase -i HEAD~5

The editor shows:

 
1pick a1b2c3d Add login page
2pick b2c3d4e Fix login validation
3pick c3d4e5f Add temporary debug logging    <-- delete this line
4pick d4e5f6g Add logout feature
5pick e5f6g7h Update tests

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

MethodChanges to filesHistoryBest for
git reset --soft HEAD~NKept (staged)Commits removedSquashing commits before push
git reset --mixed HEAD~NKept (unstaged)Commits removedRe-organizing changes into new commits
git reset --hard HEAD~NDiscardedCommits removedCompletely abandoning work
git revert HEADReversed by new commitPreserved + new commitUndoing pushed commits (or audit trail)
git rebase -i HEAD~NDepends on actionRewrittenRemoving 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:

bash
1# Show recent HEAD movements
2git reflog
3# Output:
4# a1b2c3d HEAD@{0}: reset: moving to HEAD~3
5# f4d5e6a HEAD@{1}: commit: Important work I need back
6# c3b2a1d HEAD@{2}: commit: Another commit
7
8# Restore to the state before the reset
9git reset --hard HEAD@{1}

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:

bash
1# Fetch the latest remote state
2git fetch origin
3
4# Reset local branch to match remote exactly
5git reset --hard origin/main

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.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions