Sub topic 4
Choosing the commit to reset to
This chapter assumes you can run git init, git add, git commit, git status, and git log, and that you know a branch is a movable pointer to a commit. Every git reset form needs a target commit. Picking the wrong one is the most common source of surprises, so it is worth learning the ways to name one.
What a target means
git reset <commit> moves the current branch so it points at <commit>. Commits after that point stop being reachable from the branch. If you omit the target, Git uses HEAD, which is why a bare git reset moves nothing and only resets the index.
Relative names with `~` and `^`
The most common target is relative to HEAD.
HEAD~1is the first parent ofHEAD, the commit before the tip.HEAD~3goes back three first-parent steps.HEAD^is the same asHEAD~1.HEAD^2means the second parent of a merge commit. It is not "two back".
On a straight line of history, ~n is the form to use.
Hashes and branch names
You can also use a commit hash, full or abbreviated, as long as the abbreviation is unambiguous. A branch name or tag works too, and it resolves to the commit that name points at.
git log --oneline
git reset --soft 3f2a9c1
git reset --soft main
git reset --soft v1.0Resetting to a branch name is useful for returning a feature branch to where main is. origin/main is a remote-tracking branch, and it resolves the same way.
Checking before you move
Resolve the name first, so you know where it lands. git rev-parse prints the full hash, and git log shows what would be removed.
git rev-parse HEAD~2
git log --oneline HEAD~2..HEADThe range HEAD~2..HEAD lists the commits that a reset to HEAD~2 would drop from the branch.
The target is not the same as the change you want to keep
A frequent confusion is counting commits. To remove the last commit, the target is the commit before it, HEAD~1. To remove the last three, the target is HEAD~3. The target is the commit you want to end up on, not the one you want to remove.
| Goal | Target |
|---|---|
| Remove the last commit | `HEAD~1` |
| Remove the last three commits | `HEAD~3` |
| Return to a known good commit | its hash |
| Match the remote branch | `origin/main` |
Worked example
Starting from the demo repository with three commits, check the history and then move back one commit.
git log --oneline
git rev-parse --short HEAD~1
git reset --soft HEAD~1
git log --onelineThe first git log shows three commits. After the reset, the second git log shows two, and the "third" commit's change is still staged. The commit object itself still exists, which is what makes recovery possible in a later chapter.
Common errors
- Using
HEAD^2for "two back". It selects a merge's second parent. UseHEAD~2. - Off-by-one counts. Use
git log --oneline HEAD~n..HEADto confirm that exactly the commits you expect are listed. - An ambiguous abbreviation. Git reports an error and asks for more characters. Add characters until it resolves.
- A shell that treats
^specially. In some shells, such as Windowscmd, quote the argument, for example"HEAD^". Using~avoids the problem.
Exercise
Your branch has commits A, B, C, D, with D at the tip. You want the branch to end at B. What target do you pass to git reset, and which command lists the commits that will be dropped from the branch?
Show answer
Use HEAD~2, because B is two first-parent steps behind D. List the dropped commits with git log --oneline HEAD~2..HEAD, which shows C and D.
Exercise
What is the difference between HEAD~2 and HEAD^2?
Show answer
HEAD~2 is the commit two first-parent steps back. HEAD^2 is the second parent of the current commit, which exists only when HEAD is a merge commit.