The Working Directory and the Staging Area
Asha has edited two recipes: she fixed a typo in chai.md and started a half-finished rewrite of pancakes.md. She wants to save only the typo fix. Git makes that possible because a change passes through three places before it becomes history, and you choose what moves between them.
The rule
Edit in the working directory, choose in the staging area, keep in history.
- Working directory: the real files in your folder. This is where you edit. Git notices changes here but has not saved them.
- Staging area (also called the index): a waiting room for the next snapshot.
git addcopies a file's current content here. - Repository history: the commits stored in
.git.git committurns whatever is staged into a permanent snapshot.
git add chai.md # working directory -> staging area
git commit -m "Fix typo in chai recipe" # staging area -> historygit status tells you where everything stands:
git status
# Changes to be committed:
# modified: chai.md
# Changes not staged for commit:
# modified: pancakes.md"To be committed" means staged. "Not staged" means changed in the working directory only. Untracked files are ones Git has never recorded.
To see the actual lines, use two forms of git diff:
git diffcompares the working directory with the staging area: what you changed but have not staged.git diff --stagedcompares the staging area with the last commit: what the next commit will contain.
And to move things back:
git restore --staged chai.mdunstages the file, keeping your edits.git restore pancakes.mdthrows away unstaged edits and returns the file to its staged or committed state. This cannot be undone, so use it with care.