Sub topic 9
Everyday recipes
This chapter collects the four jobs people use git reset for most: undoing a commit, unstaging files, squashing local commits, and discarding changes. It assumes you know init, add, commit, status, and log. It also assumes you know what the three areas are (the HEAD commit, the index, and the working directory) and what --soft, --mixed, and --hard change in each. Each recipe below names the mode it needs and why.
Set up a practice repository
Every recipe starts from the same three-commit history. Run this once, and rebuild it whenever a recipe leaves you somewhere you don't want to be.
git init demo && cd demo
echo one > file.txt
git add file.txt
git commit -m "first"
echo two >> file.txt
git commit -am "second"
echo three >> file.txt
git commit -am "third"
git log --onelineThe log shows three commits, newest first. file.txt contains the lines one, two, and three.
Choosing a mode for the job
Pick the mode by asking what should happen to the changes in the commit you are removing.
| Goal | Command | Commit removed | Changes end up |
|---|---|---|---|
| Redo the commit with a different message or content | `git reset --soft HEAD~1` | Yes | Staged |
| Take the commit apart and restage selectively | `git reset HEAD~1` | Yes | Unstaged in the working directory |
| Throw the commit and its changes away | `git reset --hard HEAD~1` | Yes | Gone from the working tree |
| Unstage a file | `git reset -- file.txt` | No | Unstaged, contents kept |
| Combine several commits | `git reset --soft HEAD~3` | Yes | Staged, ready for one commit |
Recipe 1: Undo the last commit and keep the changes staged
Use this when the commit content is right but the message is wrong, or when you forgot to include a file. The branch moves back one commit, and the index and working directory stay as they were.
git reset --soft HEAD~1
git statusgit status now lists the changes from the removed commit under "Changes to be committed". You can add a forgotten file with git add and run git commit again with a new message.
For a message-only fix on the latest commit, git commit --amend is shorter. Reset is the better tool when you want to go back more than one commit or when you want to inspect the staged result before recommitting.
Recipe 2: Undo the last commit and keep the changes unstaged
Use this when the commit mixed unrelated changes and you want to split it into several smaller commits. Plain git reset runs the default --mixed mode. It moves the branch and resets the index to match the new HEAD, but it leaves the working directory alone.
git reset HEAD~1
git statusThe changes from the removed commit appear under "Changes not staged for commit". Nothing is lost. You can now stage and commit them in pieces:
git add -p
git commit -m "Describe the first logical change"
git add file.txt
git commit -m "Describe the second logical change"git add -p walks through each hunk and asks whether to stage it. This is the usual way to split a commit.
A new file that the removed commit added becomes untracked after a mixed reset. Git no longer has an index entry for it, but the file stays on disk. Remember to git add it again if it belongs in one of the new commits.
Recipe 3: Unstage a file
Use this when you ran git add on a file by mistake. Resetting a path copies that path's entry from HEAD into the index. The working directory is not touched, so your edits stay.
git add file.txt
git reset -- file.txt
git statusThe -- separates options from paths. It prevents Git from reading a file name as a branch name when both exist. A path reset never moves the branch, and it does not accept --soft or --hard.
On Git 2.23 and later, git restore --staged file.txt does the same thing, and many people find the name clearer. Both are correct.
Recipe 4: Squash the last few local commits into one
Use this to tidy a series of small "wip" or "fix typo" commits before you share the branch. Suppose the last three commits belong together.
git reset --soft HEAD~3
git commit -m "Combine the last three commits"The soft reset moves the branch back three commits and leaves all of their combined changes in the index. A single new commit then captures them. Check the result with git log --oneline. The three originals are replaced by one.
The new commit has a new hash. If you had already pushed the original three, your local branch no longer matches the remote one, and a plain git push will be rejected. See the safety section below before you force anything.
Counting the right number of commits
The number after ~ is how many commits to remove from the tip. Check it before you reset:
git log --oneline -5Count from the top. If the commits you want to combine are the top three entries, use HEAD~3. Resetting to the commit just before them also works, and it uses that commit's hash instead of a count:
git reset --soft 3f2a1bcUsing a hash avoids miscounting when the history is long.
A soft reset squashes only commits that sit at the tip of the branch. To squash commits from the middle of the history, use git rebase -i instead.
Recipe 5: Discard the last commit and its changes
Use this when the commit was a mistake and you want nothing from it. The --hard mode moves the branch and overwrites both the index and the tracked files in the working directory.
git reset --hard HEAD~1
git statusThe working tree now matches the commit before the removed one, and git status reports a clean tree.
Recipe 6: Throw away all uncommitted changes
Use this to return to the last commit without removing any commits. Reset to HEAD, which means "the commit I am already on".
git reset --hard HEADThe branch does not move, because the target is where it already points. The index and the tracked files are rewritten to match HEAD. Untracked files are left alone. To remove those too, use git clean, and preview first:
git clean -nThe -n flag lists what would be deleted without deleting it. Run git clean -f only after you have read the list.
To discard changes to one file instead of the whole tree, use git restore file.txt. A path reset cannot do this, because git reset -- file.txt changes only the index.
Recipe 7: Go back to where you were before a reset
Mistakes happen. Two tools recover commits that a reset removed. The commits remain in the object database until Git cleans up unreachable objects, which by default takes weeks.
ORIG_HEAD holds the position HEAD had before the most recent reset:
git reset --hard ORIG_HEADFor older positions, read the reflog, which records where HEAD has pointed:
git reflog
git reset --hard HEAD@{1}HEAD@{1} means "where HEAD was one move ago". Read the git reflog output before you pick an entry, because other operations such as commits and checkouts also add entries.
Neither tool recovers uncommitted edits that a --hard reset overwrote, and neither recovers untracked files deleted by git clean.
Common mistakes
- Resetting a branch that others have pulled. A reset rewrites history. If teammates already have the old commits, their next pull conflicts with yours. On shared branches, use
git revert, which adds a new commit that undoes an earlier one. - Using
--hardwhen--mixedwould do. If you are unsure, use the default mode. It keeps your edits on disk, and you can always discard them afterwards. - Forgetting that a reset changes only the local branch. Commits on the remote are untouched until you push, and a push after a reset needs a force option such as
--force-with-lease. Use it only on branches that you alone use. - Miscounting
HEAD~n. Rungit log --onelinefirst and confirm which commit will become the new tip. - Expecting a path reset to touch files.
git reset -- pathchanges only the index.
Quick reference
git reset --soft HEAD~1 # undo commit, keep changes staged
git reset HEAD~1 # undo commit, keep changes unstaged
git reset --hard HEAD~1 # undo commit, discard changes
git reset -- file.txt # unstage one file
git reset --soft HEAD~3 # prepare to squash three commits
git reset --hard HEAD # discard all uncommitted tracked changes
git reset --hard ORIG_HEAD # undo the last resetExercise
Your branch has four local commits, none pushed. The top three are "add parser", "fix parser typo", and "fix parser test", and they should be one commit. Which commands combine them, and why is --soft the right mode?
Show answer
Run git log --oneline to confirm the three commits are the top three, then git reset --soft HEAD~3 followed by git commit -m "Add parser". The soft mode moves the branch back three commits but leaves the index and working directory unchanged. The combined changes of all three commits stay staged, so one commit captures them. The fourth commit, below the three, is unaffected.
Exercise
You committed a change, then realized it mixed two unrelated fixes. You want two separate commits and you must not lose any edits. Which reset mode do you use, and what do you do next?
Show answer
Run git reset HEAD~1, the default --mixed mode. It removes the commit and unstages its changes, but it leaves the edits in the working directory. Then use git add -p (or git add <file>) to stage the first fix, commit it, and repeat for the second fix.
Exercise
After git reset --hard HEAD~2, you notice you removed the wrong commits. How do you get them back, and which edits can you not recover?
Show answer
Run git reset --hard ORIG_HEAD to return to the position before the reset, or find the earlier position with git reflog and run git reset --hard HEAD@{1}. You cannot recover uncommitted edits to tracked files that the hard reset overwrote, because they were never stored in a commit.