git reset <commit> moves the branch you're on back to that commit. The commits after it drop out of git log. What happens to their changes depends on one option, mapped onto the three places from chapter 1:
| Branch moves back | Staging area | Working folder | |
|---|---|---|---|
--soft | yes | keeps the changes, staged | keeps them |
--mixed (the default) | yes | reset: changes unstaged | keeps them |
--hard | yes | reset | reset: changes thrown away |
Undoing Add a FAQ three ways, each time starting from the same commit:
git reset --soft HEAD~1
git status -s
A faq.html ← still stagedgit reset HEAD~1
git status -s
?? faq.html ← in the folder, not stagedgit reset --hard HEAD~1
HEAD is now at 3338d7e Add a banner
git status -sAfter --hard, git status -s prints nothing: faq.html is gone from the folder too.
Each HEAD~1 moves the branch back one more commit. To unstage without moving the branch, use git reset with no commit.
--mixed is the default, so git reset HEAD~1 and git reset --mixed HEAD~1 do the same. git reset with no commit resets to HEAD: it unstages everything and keeps your edits.
The undone commits fade to ghosts on the graph. They still exist; the next lessons bring them back. --hard is the one to be careful with: edits you never committed are gone for good.
Committed on the wrong branch? Label the work first, so it has a name, then move main back: git branch feature/x, then git reset --hard HEAD~2 on main (for two commits).
Objectives
- Take back the last commit, keeping its change staged.
- Unstage it, keeping the edit.
- Throw away the cart commit and the edit.