Sub topic 3
What git reset actually does
This chapter builds the working model for git reset: what it changes, in what order, and where it stops. It assumes you can use git init, add, commit, status, and log, that you know what a branch is, and that you are comfortable in a terminal. It also assumes you have read the previous chapter on the three areas Git tracks: the HEAD commit, the index, and the working directory.
One command, three possible steps
git reset has a reputation for being confusing because it is usually taught as three separate commands: soft, mixed, and hard. They are one command that runs up to three steps in a fixed order. The flag decides how many steps run.
- Move the branch. The current branch is pointed at the target commit. HEAD follows because it points at the branch.
- Reset the index. The index (staging area) is overwritten so that it matches the commit the branch now points to.
- Reset the working directory. The tracked files on disk are overwritten so that they match the index.
The steps always run in this order. A mode only decides where to stop.
| Mode | Step 1: move branch | Step 2: reset index | Step 3: reset working directory |
|---|---|---|---|
| `--soft` | yes | no | no |
| `--mixed` (default) | yes | yes | no |
| `--hard` | yes | yes | yes |
Each later chapter in this tutorial covers one mode in detail. Here the goal is to understand the shared mechanism, so that you can predict the result of any invocation before you run it.
The command's shape
The general form is:
git reset [--soft | --mixed | --hard] [<commit>]Both parts are optional. With no mode, Git uses --mixed. With no commit, Git uses HEAD, which means "the commit the branch already points to". So git reset on its own moves nothing in step 1, resets the index in step 2, and stops. That is why plain git reset unstages everything without touching your files.
The target can be any name that resolves to a commit: HEAD~1, a branch name, a tag, or an abbreviated hash. The next chapter covers these forms.
Setting up an experiment
The examples below use a small repository with three commits. Run this once and keep the directory for the exercises.
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, with "third" at the top. The branch (usually main or master, depending on your configuration) points at "third".
Step 1: what "move the branch" means
HEAD is normally not a commit hash. It is a symbolic reference, a pointer to a branch name. The branch in turn holds a commit hash. You can see both layers:
git symbolic-ref HEAD
git branch --show-current
git rev-parse HEAD "$(git branch --show-current)"The first command prints something like refs/heads/main. The last prints the same hash twice, because HEAD resolves through the branch to a commit.
When you run git reset --soft HEAD~1, Git rewrites the hash stored in the branch. HEAD still points at the branch name, so HEAD now resolves to the older commit. Nothing about HEAD itself changed.
This is the key difference from git checkout <commit> and git switch --detach <commit>. Those commands change what HEAD points to and leave the branch where it was. Reset drags the branch itself to a new place.
Reset does not delete commits
The commit that the branch used to point to still exists in the object database. It is only unreachable from the branch. Git keeps it until garbage collection removes unreachable objects, and by default the reflog protects it for a while (30 days for unreachable entries, 90 days for reachable ones, under the default gc.reflogExpire settings). A later chapter shows how to recover it.
Before moving the branch, reset saves the old position in a ref called ORIG_HEAD. You can see this directly:
git rev-parse --short HEAD
git reset --soft HEAD~1
git rev-parse --short HEAD ORIG_HEADThe first command prints the hash of "third". After the reset, HEAD prints the hash of "second" and ORIG_HEAD prints the hash of "third".
Detached HEAD
If HEAD is detached (it holds a commit hash directly, not a branch name), there is no branch to move. Reset then moves HEAD itself. The three steps still apply in the same order. Be careful here: with no branch pointing at the old commit, the only thing keeping it reachable is the reflog.
Step 2: what "reset the index" means
The index is the snapshot that your next git commit will record. Resetting it replaces every entry with the version from the commit that HEAD now points to.
Think of it as the index becoming a copy of HEAD's tree. Anything you had staged and not committed is dropped from the index. The file contents are not lost yet, because they are still on disk in the working directory, which step 3 has not touched.
This explains the common result of a mixed reset. After git reset HEAD~1, the changes from the removed commit show up in git status as unstaged modifications. The index matches the older commit, but the files on disk still contain the newer content. The difference between those two is exactly what git status reports as "Changes not staged for commit".
Step 3: what "reset the working directory" means
This step overwrites tracked files on disk so that they match the index (which, after step 2, matches HEAD). It is the only step that can destroy work that exists nowhere else.
- Uncommitted edits to tracked files are overwritten. They were never in a commit, so the reflog cannot bring them back.
- Files staged with
git addbut never committed are usually safer than plain edits. Git stored their content as a blob when you rangit add, so it is sometimes possible to find them, but that takes manual work and is not guaranteed. - Untracked files are left alone.
--hardonly touches paths that Git knows about. A new file that you never added stays on disk. - Files that are tracked in the old commit but not in the target commit are removed from disk.
Because of this asymmetry, --soft and --mixed cannot lose file contents that were committed or staged, while --hard can lose uncommitted work.
Predicting the result: three questions
Before running any reset, answer these in order.
- Where will the branch point? Resolve the target. For
HEAD~1, it is the parent of the current commit. - Which areas will change? Use the mode table. If the mode is
--mixed, expect the index to change and the files on disk to stay the same. - What will
git statusshow? Compare the new HEAD with the index, then the index with the working directory. Anything that differs in the first comparison is "staged". Anything that differs in the second is "unstaged".
You can verify answers 2 and 3 with two diff commands, which are useful for inspecting the state at any time:
git diff --cached
git diffgit diff --cached compares the index with HEAD (the staged changes). git diff compares the working directory with the index (the unstaged changes).
Reset with a path is a different operation
When you give reset a path, it does not move the branch at all. git reset -- file.txt copies the entry for that file from a commit (HEAD by default) into the index. It does not touch the working directory.
git add file.txt
git reset -- file.txt
git statusBecause the branch cannot partially move, --soft and --hard are not allowed with paths. Git refuses with an error such as Cannot do soft reset with paths. Only the mixed behavior makes sense, and it is the default. The -- separator tells Git that what follows is a path, not a commit name. It is good practice whenever a file name could also be read as a branch name.
Common mistakes
- Expecting
git resetto change the files. The default mode leaves the working directory alone. If your files did not change, that is working as designed. - Using
--hardto "undo a commit" when you meant to keep the changes. Use--softor--mixedand keep the work. - Assuming reset removes commits from the repository. It only moves a pointer. The commits remain until garbage collection.
- Resetting a branch that others have pulled. Reset rewrites where the branch points. Anyone who already fetched the old commits will have a history that conflicts with yours. A later chapter covers this.
- Forgetting that untracked files are skipped.
--harddoes not clean them up. That is a separate command,git clean, and it is destructive.
Summary
git resetmoves the current branch, and HEAD follows it.- The mode chooses how far the change spreads: branch only (
--soft), branch and index (--mixed), or branch, index, and files (--hard). - The default mode is
--mixed, and the default target isHEAD. - Reset with a path only updates the index and never moves the branch.
- Reset moves a pointer. The old commits stay in the object database, and
ORIG_HEADand the reflog record where the branch used to be.
Exercise
In the demo repository with commits "first", "second", and "third", you run git reset HEAD~1 and then git status. Which step(s) of reset ran, where does the branch point, and how does file.txt appear in the status output?
Show answer
Steps 1 and 2 ran, because the default mode is --mixed. The branch now points at "second". The index matches "second", but the file on disk still contains the "three" line from the removed commit. So file.txt shows as modified under "Changes not staged for commit".
Exercise
Why does git reset --soft -- file.txt fail, while git reset -- file.txt works?
Show answer
A reset with a path never moves the branch, so it can only update the index. The --soft mode is defined as "move the branch only", which cannot apply to a single path, and Git reports that it cannot do a soft reset with paths. Without a mode, the default mixed behavior copies the file's entry from HEAD into the index, which unstages it.