Home / Localhost / Git undo commands

Git undo: reset last commit, force pull, delete a remote branch

Undo the last commit and keep your changes, make local code match the server, and delete a remote branch, with the data-loss warnings.

Three tasks cause most git panic: undoing a commit, making your copy match the server, and deleting a branch on the server. Each has a safe command and a dangerous one, and the difference is whether your work is kept.

Pick the situation

  1. To undo a commit and keep the work: git reset --soft HEAD~1.
  2. To make your branch match the server: git fetch origin then git reset --hard origin/main (use your branch name).
  3. To delete a branch on the server: git push origin --delete branchname.
  4. Before anything destructive, make a safety branch: git branch backup-before-reset.

Commands you can copy

Undo last commit, keep changes staged
git reset --soft HEAD~1
Undo last commit, keep changes unstaged
git reset HEAD~1
Undo last commit, discard changes
git reset --hard HEAD~1
Safety copy of where you are
git branch backup-before-reset
Match the server (discards local work)
git fetch origin && git reset --hard origin/main
Delete remote branch
git push origin --delete branchname
Same, older form
git push origin :branchname
Safer force push
git push --force-with-lease

What each command does

--soft, --mixed, --hard
--soft moves only the branch pointer: your files and staged changes stay. --mixed, the default, also clears the staging area but keeps the files. --hard overwrites your files with the target commit, discarding changes.
Undo a commit and redo it
After git reset --soft HEAD~1 you can fix things and commit again. Git's own example then uses git commit -a -c ORIG_HEAD to reuse the old message.
ORIG_HEAD
Before a reset, git saves the previous tip as ORIG_HEAD. If you reset by mistake, git reset --hard ORIG_HEAD goes back, as long as you have not changed anything since.
Force pull
There is no single command. git fetch then git reset --hard origin/main throws away your local commits and any changes to tracked files so the branch equals the server. Untracked files that clash may be overwritten too.
Do not use reset --hard on shared commits
Git warns against using --hard to undo commits that others already have. Use git revert instead, which adds a new commit that undoes the old one.
Delete a remote branch
--delete (or -d) removes the ref from the remote, the same as prefixing it with a colon. Your local branch stays; remove it with git branch -d branchname.
--force and --force-with-lease
By default a push is refused if it would lose commits. --force disables that check and can cause the remote to lose commits. --force-with-lease refuses if someone else pushed in the meantime, so use it instead.
Push rejected
See git push rejected for the usual causes.

Questions about Git undo commands

How do I undo my last commit but keep my changes?
git reset --soft HEAD~1 keeps them staged. git reset HEAD~1 keeps them but unstaged.
How do I overwrite my local changes with the server version?
git fetch origin, then git reset --hard origin/branchname. This discards local work, so make a backup branch first.
How do I delete a remote branch?
git push origin --delete branchname.
Can I recover after git reset --hard?
Sometimes: git reset --hard ORIG_HEAD right away, or look in git reflog for the old commit. Uncommitted changes are gone.

Updated 2026-10-27

Related pages