# Undo a Git Commit

> Three ways to take back a commit: revert, reset and amend. Which one is safe on a branch others have, and which one can lose your work for good.

**Course:** [Git and GitHub](https://learn.benibix.com/learn/development-version-control)  
**Time:** 13 min

**After this lesson you can:**

- Undo a shared commit with revert
- Move a branch back with reset
- Fix your last commit with amend

## Which undo to use: revert, reset or amend

Git has three main ways to undo a commit. Revert adds a new commit that does the opposite. Reset moves the branch back, so later commits drop out. Amend swaps your last commit for a fixed one. Only revert is safe on a branch other people already have, because it is the only one that adds rather than rewrites.

Each one fits a different moment. A commit pushed yesterday that broke the site wants a revert. Three messy commits you made this morning and never shared can take a reset. A typo in the message you wrote a minute ago wants an amend.

Git revert takes a commit's hash and writes a new commit that undoes it. If the old commit added a line, the new one takes it out. The old commit stays in the history, so anyone can see what happened and why. Nothing is deleted, which is what makes it safe to share.

Git reset moves your branch label back to an earlier commit, and it has three modes. Soft keeps the undone changes staged. Mixed, the default, keeps them in your files but not staged. Hard throws them away, along with any changes you never committed.

Git reset --soft HEAD~1 is a handy trick. HEAD~1 means one commit before where you are. That commit disappears, but its changes stay staged, so you can commit them again with a better message, or split them up.

Git commit --amend replaces your last commit with a new one. You can change the message, add a file you forgot, or both. Git makes a new commit with a new hash, and the old one drops out of the branch. To add a forgotten file, stage it first, then amend.

Reset and amend both rewrite history. On your own branch that nobody else has, that is fine. Others may have pulled the branch, though. Their copies still hold the commits you removed, and the next push or pull turns into a mess. For anything already shared, use revert.

The costly mistake is to run git reset --hard without a second thought. The commits it takes away can often be found again for a while. Changes you never committed are gone for good. Run git status first, and stash or commit anything you might want back.

## The three undo commands

| Command | What it does | Safe once shared? |
| --- | --- | --- |
| git revert | Adds a commit that does the opposite | Yes |
| git reset | Moves the branch back to an older commit | No |
| git commit --amend | Swaps the last commit for a fixed one | No |

## Which one to pick

1. **Revert.** When the bad commit is already pushed, and other people may have it.
2. **Reset.** When the commits are only on your computer, and you want to redo them.
3. **Amend.** When only your last commit is wrong, and you have not pushed it yet.

```console
$ git log --oneline -2
e963df2 Add a search page
cb0fe9d Add footer
$ git reset --soft HEAD~1
$ git status --short
A  search.html
```
The commit is gone from the log, but its change is still staged and ready to commit again.

![Three rows, each starting from commits A, B and C in a line. Revert, lit up, adds a new commit D that undoes C, so history only grows and is safe to share. Reset moves the branch label back from C to B, so C drops out of the branch. Amend swaps C for a new C with a fixed message or file. A note says reset and amend rewrite history, so use them only on commits you have not pushed.](https://learn.benibix.com/diagrams/dev3-undo-changes.svg)

### Example: Jack takes back a bad price

On Monday Jack pushes a commit that shows every price 10 times too high. Two teammates have already pulled it.

He runs git revert on that commit's hash and pushes. A new commit puts the prices back, his teammates pull it like any other change, and the history shows both.

**Words in this lesson:** [Git Revert](https://learn.benibix.com/term/development/git-revert), [Git Reset](https://learn.benibix.com/term/development/git-reset), [Git Commit Amend](https://learn.benibix.com/term/development/git-commit-amend)

> **The one thing to remember.** Revert adds a commit, so it is safe to share. Reset and amend rewrite history, so keep them for commits nobody else has.

**Question:** Which undo is safe on a branch my team has already pulled?

**Answer:** git revert. Revert adds a new commit that undoes the old one. Nothing anyone has is taken away. Reset and amend take away commits that your teammates still have.

---

Source: https://learn.benibix.com/learn/development-version-control/undo-a-git-commit
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
