Sub topic 8
Moving a Branch with git reset: soft, mixed, and hard
This chapter assumes you can commit, branch, and read git log and git status. It also assumes you know the three areas: the working tree (your files), the index (the staging area), and HEAD (the commit your branch points to).
What reset does
git reset <commit> moves the current branch pointer to <commit>. The commits that were ahead of it are no longer reachable from the branch. The mode you choose decides how far the change spreads from there.
--softmoves the branch only.--mixed(the default) moves the branch and resets the index to match.--hardmoves the branch, resets the index, and overwrites the working tree.
The three modes in practice
Suppose your last commit was a mistake. Each command below moves the branch back one commit:
git reset --soft HEAD~1
git reset HEAD~1
git reset --hard HEAD~1Run only one of them, not all three in a row. After each, the undone commit's changes end up in a different place:
| Command | Branch moves | Index | Working tree | Changes from the undone commit |
|---|---|---|---|---|
| `git reset --soft HEAD~1` | Yes | Kept | Kept | Staged |
| `git reset HEAD~1` | Yes | Reset | Kept | Unstaged |
| `git reset --hard HEAD~1` | Yes | Reset | Reset | Discarded |
Use --soft to redo a commit, for example to combine the last few commits into one. Use --mixed to split a commit into smaller ones, because you can stage the pieces again with git add -p. Use --hard only when you want the work gone.
A worked example
Say you made two commits on a feature branch and want to turn them into one.
- Run
git log --onelineand confirm the two commits are at the top. - Run
git reset --soft HEAD~2. The branch moves back two commits, and all the changes from both stay staged. - Run
git commit -m "Add login form". You now have one commit with the combined changes.
Common errors
- Losing uncommitted work with
--hard. Uncommitted edits in tracked files are overwritten, and Git cannot bring them back, because they were never committed. Rungit statusfirst, and commit or stash anything you want to keep. - Resetting a branch you have already pushed. After a reset, your branch no longer contains commits the remote still has. A normal push is then rejected. Forcing the push rewrites shared history for everyone, which a later chapter covers. For pushed commits, prefer
git revert. - Confusing branch reset with file restore.
git reset HEAD~1moves the branch.git restore --staged fileonly unstages one file. If you only want to change a file, userestore. - Forgetting the commits still exist. A reset commit is not deleted right away. The reflog still lists it, so a mistaken reset can usually be undone.
Exercise
You committed a bug fix together with an unrelated debug change. You want to undo the commit, keep both edits in your files, and then commit them separately. Which reset mode do you use, and why?
Show answer
Use the default git reset HEAD~1 (--mixed). It moves the branch back and unstages the changes but keeps them in the working tree, so you can stage each part with git add and commit them separately. --soft would leave everything staged, which makes splitting harder, and --hard would delete the edits.
Exercise
After git reset --hard HEAD~2, which of the three areas (working tree, index, HEAD) changed?
Show answer
All three. The branch and HEAD move back two commits, the index is reset to match that commit, and the working tree is overwritten to match it too.