Lesson 03 of 11Concept
HEAD, Staging Area and Working Tree
Goal: Understand the three places a file lives, so git reset and git revert make sense.
Three copies of every file
Git keeps up to three versions of each tracked file:
- HEAD: the version in the commit your branch points at.
- Staging area (the index): the version the next commit will record.
- Working tree: the file on disk, the one you edit.
git status is a report of where these three disagree. Undoing something in Git always means choosing which of the three to move back.
What git reset moves
--soft
- HEAD: moves the branch back
- Staging area: unchanged, so the undone commit's changes are staged
- Working tree: unchanged
--mixed (default)
- HEAD: moves the branch back
- Staging area: matches the new HEAD
- Working tree: unchanged, so the changes are unstaged edits
--hard
- HEAD: moves the branch back
- Staging area: matches the new HEAD
- Working tree: matches too; uncommitted work is gone
git reset --soft HEAD~1
Step 1 of 3
main points at C, which added login.js. Everything is clean.
Step 1 of 3. `main` points at C, which added `login.js`. Everything is clean.
Reset or revert?
git reset
- Moves the branch back; later commits leave the history
- Rewrites history, so only use it on commits nobody else has
- Good for local mistakes
git revert
- Adds a new commit that undoes an old one
- History only grows, so it is safe on shared branches
- Good for undoing something already pushed
