Git Reset in Depth
git reset is the command that moves the current branch to another commit, and then, depending on its mode, makes the staging area and the working tree match that commit too. To understand it fully, you need the idea of Git's three trees: three versions of your files that Git keeps at the same time. Each reset mode changes a different number of them, which is the whole difference between --soft, --mixed and --hard.
"Git Reset: Soft, Mixed and Hard" showed what each mode does to a commit. This lesson looks underneath: it puts a different version of one file in each of the three trees, runs every mode, and shows exactly which versions change.
The Three Trees
| Tree | What it holds | Look at it with |
|---|---|---|
| HEAD | The snapshot of the last commit on the current branch | git show HEAD:<file> |
| Index (staging area) | The snapshot the next commit will have | git show :<file> |
| Working tree | The files in your folder, as you edit them | cat <file> |
In everyday words: HEAD is "what I committed", the index is "what I'm about to commit", and the working tree is "what I'm editing". git status compares them: index vs HEAD gives "Changes to be committed", working tree vs index gives "Changes not staged".
A File in Four Versions
To see every tree at once, Navika puts a different line in each. Version 1 and version 2 are committed, version 3 is staged, and version 4 is only in the folder:
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo 'v1: Term 1 fee is $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Version 1"
echo 'v2: Term 1 fee is $12,500' > fees.txt
git commit --quiet -am "Version 2"
echo 'v3: Term 1 fee is $13,000' > fees.txt
git add fees.txt
echo 'v4: Term 1 fee is $13,500' > fees.txt
echo "HEAD: $(git show HEAD:fees.txt)"
echo "Index: $(git show :fees.txt)"
echo "Working tree: $(cat fees.txt)"
git log --onelineHEAD: v2: Term 1 fee is $12,500
Index: v3: Term 1 fee is $13,000
Working tree: v4: Term 1 fee is $13,500
63c9fc4 (HEAD -> main) Version 2
245beda Version 1The three lines show the three trees: HEAD has v2, the index has v3 and the working tree has v4. Each example below starts from this state and resets to HEAD~1, "Version 1".
Example 1: --soft Moves HEAD Only
git reset --soft HEAD~1
echo "HEAD: $(git show HEAD:fees.txt)"
echo "Index: $(git show :fees.txt)"
echo "Working tree: $(cat fees.txt)"HEAD: v1: Term 1 fee is $12,000
Index: v3: Term 1 fee is $13,000
Working tree: v4: Term 1 fee is $13,500Explanation: only HEAD changed: the branch now points to "Version 1". The index still has v3 and the folder still has v4. Nothing you staged or edited is touched. git reset --soft ORIG_HEAD moves the branch back to "Version 2", which restores the starting state exactly:
git reset --soft ORIG_HEAD
git log --oneline -163c9fc4 (HEAD -> main) Version 2Example 2: --mixed Moves HEAD and Resets the Index
git reset --quiet --mixed HEAD~1
echo "HEAD: $(git show HEAD:fees.txt)"
echo "Index: $(git show :fees.txt)"
echo "Working tree: $(cat fees.txt)"HEAD: v1: Term 1 fee is $12,000
Index: v1: Term 1 fee is $12,000
Working tree: v4: Term 1 fee is $13,500Explanation: HEAD moved to v1, and the index was reset to match it: the staged v3 is no longer staged. The working tree still has v4. Note that v3 now exists nowhere except in Git's object store as an unreferenced blob: it was staged, never committed, and the reset replaced it. --mixed is the default mode.
To start the next example from the same state, the four versions are rebuilt:
git reset --quiet --hard ORIG_HEAD
echo 'v3: Term 1 fee is $13,000' > fees.txt
git add fees.txt
echo 'v4: Term 1 fee is $13,500' > fees.txt
git status -sMM fees.txtMM means: changed in the index (compared with HEAD), and changed again in the working tree (compared with the index). All three trees differ again.
Example 3: --hard Resets All Three
git reset --hard HEAD~1
echo "HEAD: $(git show HEAD:fees.txt)"
echo "Index: $(git show :fees.txt)"
echo "Working tree: $(cat fees.txt)"HEAD is now at 245beda Version 1
HEAD: v1: Term 1 fee is $12,000
Index: v1: Term 1 fee is $12,000
Working tree: v1: Term 1 fee is $12,000Explanation: every tree now has v1. The staged v3 and the edited v4 are gone. Only "Version 2" can be brought back, through ORIG_HEAD or the reflog, because it was committed.
| Mode | HEAD (branch) | Index | Working tree |
|---|---|---|---|
--soft | Moves | Unchanged | Unchanged |
--mixed (default) | Moves | Reset to the commit | Unchanged |
--hard | Moves | Reset to the commit | Reset to the commit |
--keep | Moves | Reset to the commit | Reset, but refuses if that would lose local changes |
A Safer Hard Reset: --keep
--keep resets like --hard, but refuses when a file you changed locally would be overwritten. Navika rebuilds the situation and tries it:
git reset --quiet --hard ORIG_HEAD
echo 'v4: Term 1 fee is $13,500' > fees.txt
git reset --keep HEAD~1error: Entry 'fees.txt' not uptodate. Cannot merge.
fatal: Could not reset index file to revision 'HEAD~1'.The reset stopped: fees.txt has local changes, and the target commit has a different fees.txt. Nothing changed. With --hard, the edit would be lost. --keep is the better habit when you only want to move a branch back and expect your working tree to be clean.
Reset with a Path
Given a path, git reset never moves HEAD. It only copies that file from a commit into the index:
git add fees.txt
git reset --quiet HEAD~1 -- fees.txt
echo "HEAD: $(git show HEAD:fees.txt)"
echo "Index: $(git show :fees.txt)"
echo "Working tree: $(cat fees.txt)"HEAD: v2: Term 1 fee is $12,500
Index: v1: Term 1 fee is $12,000
Working tree: v4: Term 1 fee is $13,500Explanation: HEAD stays at v2 and the working tree at v4. Only the index got v1, the version from HEAD~1. With no commit given, git reset <file> copies from HEAD, which simply unstages the file. The modern command for both is git restore --staged [--source=<commit>] <file>.
checkout, switch, restore and reset
git checkout used to do several of these jobs, depending on its arguments. Git 2.23 split it into two clearer commands. Here is what each one changes:
| Command | HEAD / branch | Index | Working tree | Use it to |
|---|---|---|---|---|
git switch <branch> | Moves HEAD to another branch (the branches stay) | Updated | Updated (keeps your edits) | Change branches |
git restore <file> | No | No | Copied from the index | Throw away edits |
git restore --staged <file> | No | Copied from HEAD | No | Unstage |
git reset <commit> | Moves the branch | Reset (--mixed) | Depends on mode | Undo commits |
git reset <commit> -- <file> | No | Copied from the commit | No | Unstage, or stage an old version |
git checkout <branch> (older) | Moves HEAD | Updated | Updated | Same as git switch |
git checkout -- <file> (older) | No | No | Copied from the index | Same as git restore <file> |
The key difference between switch and reset: git switch moves HEAD to point to another branch, and both branches stay where they are. git reset moves the current branch itself to another commit, which changes that branch's history.
Common Mistakes
Thinking --mixed deletes your edits. It doesn't touch the working tree. Only --hard does.
Using --hard to unstage. git reset <file> or git restore --staged <file> unstages without touching the file.
Forgetting that staged-but-uncommitted work isn't recoverable. After --mixed or --hard, a staged version that was never committed is effectively lost. Commit or stash before resetting.
Mixing up reset and switch. git reset other-branch doesn't switch branches. It moves your current branch to other-branch's commit.
Resetting a pushed branch. It rewrites shared history. Use git revert there.
Under the Hood
Each tree is stored differently. HEAD is a reference: .git/HEAD names the branch, and the branch file names a commit, whose tree is the snapshot. The index is one binary file, .git/index, listing every path with the blob ID of its staged content. The working tree is your folder. A reset is three ordered steps, and the mode decides where it stops: (1) write the target commit's ID into the branch file, (2) rebuild .git/index from the target commit's tree, (3) rewrite the working tree files from the index. --soft stops after 1, --mixed after 2, --hard runs all 3. git reset <commit> -- <path> skips step 1 and runs step 2 for that path only.
Interview Corner
What are Git's three trees?
HEAD (the last commit's snapshot), the index or staging area (the next commit's snapshot), and the working tree (the files on disk). Most commands copy content between these three.
Explain git reset --soft, --mixed and --hard in terms of the three trees.
All move HEAD's branch to the target commit. --soft changes nothing else. --mixed also resets the index to the target. --hard resets the index and the working tree to the target.
What does git reset <commit> -- <file> do?
It copies the file's version from <commit> into the index only. HEAD and the working tree don't change. Without a commit, it unstages the file. git restore --staged --source=<commit> <file> is the modern equivalent.
What is git reset --keep?
A mode like --hard that refuses to run if it would overwrite local changes in files that differ between HEAD and the target. It is a safer way to move a branch back.
What is the difference between git reset and git switch?
git switch changes which branch HEAD points to, leaving every branch where it is. git reset moves the current branch to a different commit, changing that branch's history.
Practice
Challenge: predict the trees. Rebuild the starting state (HEAD at "Version 2" with v2, v3 staged, v4 in the folder). The solution finds "Version 2" by its message with ":/Version 2". Then run git reset --soft HEAD~1 followed by git reset --mixed, with no commit for the second command. Print the three trees.
What does Git print? Before you open the solution, work out each tree step by step. A reset without a commit uses HEAD as the target.
Show solution
git reset --quiet --hard ":/Version 2"
echo 'v3: Term 1 fee is $13,000' > fees.txt
git add fees.txt
echo 'v4: Term 1 fee is $13,500' > fees.txt
git reset --soft HEAD~1
git reset --quiet --mixed
echo "HEAD: $(git show HEAD:fees.txt)"
echo "Index: $(git show :fees.txt)"
echo "Working tree: $(cat fees.txt)"HEAD: v1: Term 1 fee is $12,000
Index: v1: Term 1 fee is $12,000
Working tree: v4: Term 1 fee is $13,500--soft HEAD~1 moved HEAD to v1 and left the index (v3) and the working tree (v4) alone. --mixed with no commit kept HEAD where it was (v1) and reset the index to match it, so the staged v3 is gone. The working tree still has v4.
Checkpoint. In a practice repository, put a different line in each of the three trees, as in this lesson, and predict, then check, the three trees after each of --soft, --mixed, --hard and --keep.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git show HEAD:<file> | See a file in HEAD |
git show :<file> | See a file in the index |
git reset --soft <commit> | Move the branch only |
git reset <commit> | Move the branch and reset the index |
git reset --hard <commit> | Move the branch and reset everything |
git reset --keep <commit> | Like --hard, but keep local changes safe |
git reset <commit> -- <file> | Copy a file's version into the index |
Was this tutorial helpful?