Sub topic 3
What git reset actually does
This chapter assumes you can use git add, git commit, git status, and git log. It also assumes you know that a branch is a movable name for a commit. You should know what HEAD, the index, and the working directory are from the previous chapter.
One command, up to three steps
git reset <commit> makes the current branch point at <commit>. It then, depending on the mode you choose, copies that commit's snapshot into the index and the working directory.
You can think of it as three steps that run in order and stop at the point the mode allows:
- Move the branch that HEAD points to, so it names the target commit.
- Reset the index to match the snapshot of the new commit.
- Reset the working directory to match the index.
The mode decides how many steps run. The table below summarizes them. Later chapters cover each mode in detail.
| Mode | Moves branch | Resets index | Resets working directory |
|---|---|---|---|
| `--soft` | Yes | No | No |
| `--mixed` (default) | Yes | Yes | No |
| `--hard` | Yes | Yes | Yes |
Step 1 happens in every mode
When you are on a branch, HEAD does not point at a commit directly. It points at the branch name, and the branch name points at a commit. git reset rewrites the branch name's target. HEAD still points at the same branch.
Suppose main has three commits. After git reset HEAD~1, main points at the second commit. The third commit is not deleted. It is just no longer reachable from main, so git log stops showing it.
Before it moves anything, Git saves the old position of HEAD in ORIG_HEAD. This gives you a way back, which a later chapter covers.
Watching the branch move
Start from the three-commit repository built earlier in this tutorial. Check the history, reset one step with --soft, and look again.
git log --oneline
git reset --soft HEAD~1
git log --oneline
git status --shortThe output looks like this. Your hashes will differ.
c3d4e5f third
b2c3d4e second
a1b2c3d first
b2c3d4e second
a1b2c3d first
M file.txtThe third commit has disappeared from the log, so step 1 ran. file.txt still contains all three lines, and the M in the left column means the change is staged. Steps 2 and 3 did not run, so the index and working directory still hold the content of the removed commit.
Run the same experiment with git reset HEAD~1 (mixed) and the M moves to the right column, which means unstaged. With --hard, the file reverts to two lines and git status reports a clean tree.
Reset is not checkout
git checkout <commit> and git switch move HEAD to a different branch or commit, and they leave branch names alone. git reset does the reverse. HEAD keeps pointing at the same branch, and the branch itself is moved. This is why reset can change the history of a branch, and why it needs care once a branch is shared.
Common errors
- Expecting
git reset HEAD~1to keep your changes staged. The default mode is--mixed, so the changes come back unstaged. Add--softto keep them staged. - Assuming a reset deleted commits permanently. The commits still exist and can be reached through the reflog until Git cleans them up.
- Running
--hardwith uncommitted edits. Commit or stash first if you might want those edits.
Exercise
You are on main with commits A, B, and C (C is newest). You run git reset --soft A. Where does main point, what is staged, and what does git log show?
Show answer
main points at A. The changes from B and C are staged in the index, and the working directory still contains them. git log shows only A.