Git Reset in Depth

Updated · 11 min read
hardGit 2.53gitgit-resetthree-treesrestoreswitchinterview

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

TreeWhat it holdsLook at it with
HEADThe snapshot of the last commit on the current branchgit show HEAD:<file>
Index (staging area)The snapshot the next commit will havegit show :<file>
Working treeThe files in your folder, as you edit themcat <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:

Terminal
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 --oneline
Output
HEAD:         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 1

The 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

Terminal
git reset --soft HEAD~1
echo "HEAD:         $(git show HEAD:fees.txt)"
echo "Index:        $(git show :fees.txt)"
echo "Working tree: $(cat fees.txt)"
Output
HEAD:         v1: Term 1 fee is $12,000
Index:        v3: Term 1 fee is $13,000
Working tree: v4: Term 1 fee is $13,500

Explanation: 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:

Terminal
git reset --soft ORIG_HEAD
git log --oneline -1
Output
63c9fc4 (HEAD -> main) Version 2

Example 2: --mixed Moves HEAD and Resets the Index

Terminal
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)"
Output
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

Explanation: 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:

Terminal
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 -s
Output
MM fees.txt

MM 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

Terminal
git reset --hard HEAD~1
echo "HEAD:         $(git show HEAD:fees.txt)"
echo "Index:        $(git show :fees.txt)"
echo "Working tree: $(cat fees.txt)"
Output
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,000

Explanation: 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.

ModeHEAD (branch)IndexWorking tree
--softMovesUnchangedUnchanged
--mixed (default)MovesReset to the commitUnchanged
--hardMovesReset to the commitReset to the commit
--keepMovesReset to the commitReset, 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:

Terminal
git reset --quiet --hard ORIG_HEAD
echo 'v4: Term 1 fee is $13,500' > fees.txt
git reset --keep HEAD~1
Output
error: 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:

Terminal
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)"
Output
HEAD:         v2: Term 1 fee is $12,500
Index:        v1: Term 1 fee is $12,000
Working tree: v4: Term 1 fee is $13,500

Explanation: 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:

CommandHEAD / branchIndexWorking treeUse it to
git switch <branch>Moves HEAD to another branch (the branches stay)UpdatedUpdated (keeps your edits)Change branches
git restore <file>NoNoCopied from the indexThrow away edits
git restore --staged <file>NoCopied from HEADNoUnstage
git reset <commit>Moves the branchReset (--mixed)Depends on modeUndo commits
git reset <commit> -- <file>NoCopied from the commitNoUnstage, or stage an old version
git checkout <branch> (older)Moves HEADUpdatedUpdatedSame as git switch
git checkout -- <file> (older)NoNoCopied from the indexSame 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.

What each command changes

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
Terminal
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)"
Output
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.

Quick check: Reset in depth

4 questions
1Which tree does git reset --soft change?
2After git reset --mixed HEAD~1, what happens to a staged change?
3What does git reset HEAD~1 -- fees.txt change?
4Why prefer --keep over --hard when moving a branch back?

Key Takeaways

Quick reference

CommandIt 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?

Lesson 62 of 106