What you can do after this lesson
You will fix the most common small mistakes (a typo in a message, a forgotten file) and you will understand git reset, the command people fear most. The fear is reasonable but fixable: once you know what each mode touches, reset becomes predictable. Examples use a fictional shopping list with three commits. Hashes on your machine will differ.
Amending the last commit
git commit --amend replaces the latest commit rather than adding a new one. To fix a message:
git commit --amend -m "Add salt to the shopping list"
git log --oneline[main 0b9e972] Add salt to the shopping list
Date: Tue Nov 10 10:00:00 2026 +0000
1 file changed, 1 insertion(+)
0b9e972 Add salt to the shopping list
f2afcbf Add yeast
4c52fa5 Add shopping listCompare this with the commit before the amend: the message is new, and the hash is also new. That is not a quirk. A commit's identity comes from its content, so changing anything creates a different commit, and the old one is simply abandoned.
To add a forgotten file, stage it and amend without changing the message:
echo "sugar" > extra.txt
git add extra.txt
git commit --amend --no-edit
git show --stat --oneline[main ffbb9f2] Add salt
Date: Tue Nov 10 10:00:00 2026 +0000
2 files changed, 2 insertions(+)
create mode 100644 extra.txt
ffbb9f2 Add salt
extra.txt | 1 +
shopping.txt | 1 +
2 files changed, 2 insertions(+)The commit now holds both files. Golden rule: amend only commits you have not shared. If someone already pulled the old commit, your new version conflicts with theirs. Shared mistakes are fixed with git revert, covered in the next lesson.
What git reset does
A branch is a label pointing at a commit. git reset <commit> moves the current branch label to another commit. What happens to your files depends on the mode:
| Mode | Branch moves | Staging area | Working tree |
|---|---|---|---|
--soft |
yes | unchanged | unchanged |
--mixed (default) |
yes | reset to match | unchanged |
--hard |
yes | reset to match | reset to match |
Think of it as how far the reset reaches through the three areas from lesson 3: soft touches only the repository, mixed also touches the staging area, hard reaches all the way into your files.
Let us reset from the same starting point three times. In each case the history was Add shopping list, Add yeast, Add salt.
Soft: undo the commit, keep everything staged
git reset --soft HEAD~1
git log --oneline
git status -s
cat shopping.txtf2afcbf Add yeast
4c52fa5 Add shopping list
M shopping.txt
buy flour
buy yeast
buy saltThe Add salt commit is gone from the log, but its change is still staged and still in the file. This is ideal for squashing: reset soft over several commits and make a single new one from the combined changes.
Mixed: undo the commit, keep the edits unstaged
git reset HEAD~1
git status -s
git diffUnstaged changes after reset:
M shopping.txt
M shopping.txt
diff --git a/shopping.txt b/shopping.txt
index 21c118a..a286201 100644
--- a/shopping.txt
+++ b/shopping.txt
@@ -1,2 +1,3 @@
buy flour
buy yeast
+buy saltMixed is the default when you give no mode. The commit is undone and the change returned to your working tree, ready to be re-staged selectively and recommitted.
Hard: undo the commit and discard the changes
echo "scribble" >> shopping.txt
git status -s
git reset --hard HEAD~1
git status -s
cat shopping.txt M shopping.txt
HEAD is now at f2afcbf Add yeast
buy flour
buy yeastBoth the commit and the uncommitted scribble vanished, and the status is clean. Of the three modes this is the only one that can lose work, and the unstaged scribble is lost for good. The committed Add salt change is not: Git still holds it, and the reflog finds it.
git reflog
git reset --hard HEAD@{1}
cat shopping.txtf2afcbf HEAD@{0}: reset: moving to HEAD~1
aa986bd HEAD@{1}: commit: Add salt
f2afcbf HEAD@{2}: commit: Add yeast
4c52fa5 HEAD@{3}: commit (initial): Add shopping list
HEAD is now at aa986bd Add salt
buy flour
buy yeast
buy saltHEAD@{1} means "where HEAD was one move ago". The next lesson digs into reflog properly.
Resetting to other targets
You are not limited to HEAD~1. git reset --hard origin/main makes your branch match the remote's exactly, and git reset --soft <hash> rewinds to any commit by its hash. Resetting also accepts file paths (git reset shopping.txt), in which case it unstages that file, doing the same job as git restore --staged.
When rewriting is safe
Amend and reset rewrite your own branch. That is fine for local, unshared commits, and you will often do it to tidy up before sharing. Once other people have fetched the commits, rewriting forces them to repair their copies. For shared history, add a new commit that undoes the change instead.
Choosing between the tools
A quick rule of thumb: if the problem is in the last commit only, amend it. If the problem spans several recent commits that nobody else has, use a soft or mixed reset and recommit them in a better shape. If you want to throw away both commits and edits, use a hard reset, but only after git status shows nothing you care about. If anyone else might have the commits, stop and use revert instead. Pausing to classify the situation takes a moment and avoids most regrets.
Common mistakes
- Using
--hardwith uncommitted work present. Rungit statusfirst, and stash or commit what you want to keep. - Amending a pushed commit. You will need a force push and your teammates will have to reconcile.
- Forgetting the default is mixed.
git reset HEAD~1leaves your changes unstaged, so a followinggit commitfinds nothing to commit. - Typing
HEAD~with the wrong number. Checkgit log --onelinefirst. - Panicking after a hard reset of committed work. Look at
git reflogbefore anything else.
Exercise
- Make three commits in a scratch repository.
- Reword the last message with
--amend. - Use
git reset --soft HEAD~2and make one commit from the two undone ones. - Make a deliberate mistake with
git reset --hard HEAD~1and recover with reflog.
Show one possible answer
git commit --amend -m "Clearer message"
git reset --soft HEAD~2
git commit -m "Combine the last two changes"
git reset --hard HEAD~1
git reflog
git reset --hard HEAD@{1}After the final command your branch is back where it was before the hard reset.
Next: Undoing shared history with revert, and finding lost work with reflog.
Key points
- git commit --amend replaces the latest commit with a new one, so it changes the hash and should not be used on commits others already have.
- git reset moves the current branch to another commit; the mode decides what happens to the staging area and working tree.
- --soft keeps changes staged, --mixed (the default) keeps them unstaged, and --hard discards them.
- Only --hard (and git clean) can lose uncommitted work; the commits themselves usually stay recoverable through reflog.
- Rewriting history is safe for commits that exist only on your machine and risky for shared ones.