Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 41Git · CI/CD

How to undo the last Git commit

A wrong file in the commit, a bad message, a commit on the wrong branch, or a change that broke production. "Undo" in Git means different commands — and the most important question is: did you push it already? If not, you can rewrite your history freely. If yes, you add a new commit that undoes the old one. This how-to gives the right command for each case, and how to get back a commit you thought was lost.

  • 5 Lessons
  • 1 Commands

$ run as a normal user# run as root (or with sudo)

41.1

The quick answer

git reset --soft HEAD~1
git commit --amend -m "Fix the login redirect"
git revert HEAD
git reflog
CaseCommand
Not pushed — undo the commit, keep the changesgit reset --soft HEAD~1
Not pushed — only fix the message or add a filegit commit --amend
Already pushed — undo it for everyonegit revert HEAD
Something went wrong — find the lost commitgit reflog

Commands in this lesson

41.2

Step by step

  1. Check where you are — branch, last commits, and if they are pushed:
git status -sb
git log --oneline -3

If the first line of git status -sb ends with [ahead 1], one commit is not pushed yet.

  1. Not pushed: undo the commit. Choose what happens to the changes:
CommandThe commitYour changes
git reset --soft HEAD~1GoneStill staged — ready to commit again.
git reset HEAD~1 (mixed)GoneIn the files, not staged.
git reset --hard HEAD~1GoneDeleted — careful.
  1. Not pushed: fix the last commit (forgot a file, typo in the message):
git add forgotten-file.py
git commit --amend --no-edit
git commit --amend -m "Add rate limit to the login API"
  1. Commit on the wrong branch (not pushed): move it to a new branch, then remove it from main:
git branch feature/login-limit
git reset --hard origin/main
git switch feature/login-limit
  1. Already pushed: revert. This makes a new commit that does the opposite — safe for shared branches:
git revert HEAD
git revert 3f9c2e1
git revert -m 1 8a1b2c3
git push

-m 1 is needed to revert a merge commit (keep the first parent = main).

  1. Lost something? reflog lists every place HEAD was, also after a reset --hard:
git reflog -5
a1f8ed1 HEAD@{0}: reset: moving to HEAD~1
7c14206 HEAD@{1}: commit: Add rate limit to the login API
git reset --hard 7c14206
  1. Undo changes that are not committed (in files, or staged):
git restore app.py
git restore --staged app.py

Commands in this lesson

41.3

Check that it worked

git log --oneline -3
git status

The commit is gone (reset) or a "Revert …" commit is on top (revert), and the files look the way you want. After a revert, CI runs again on the reverted code.

Commands in this lesson

41.4

Common problems

ProblemCauseFix
rejected … non-fast-forward after a reset/amendYou rewrote history that was already pushed.Use git revert instead; only on your own branch: git push --force-with-lease.
reset --hard deleted workUncommitted changes are not in the reflog.Committed work: git reflog. Never-committed changes are lost — commit or git stash first.
error: commit … is a merge but no -m option was givenReverting a merge commit.git revert -m 1 HASH.
Revert of a revert is needed laterYou want the change back.git revert the revert commit.
A secret was in the commitReset or revert does not remove it from the remote history.Rotate the secret, then clean the history.
Teammates have the old history after your force-pushThey pulled before.Avoid force-push on shared branches; they must reset to the new remote.

Commands in this lesson

41.5

Go further

1The commit is already pushed to main. Reset or revert?

Revert — it adds a new commit and does not rewrite shared history.

2You ran git reset --hard and lost a commit. How do you get it back?

Commands in this lesson

All commands in this chapter