Sub topic 1
The three places your changes live
Prerequisites
This chapter assumes you can use git add, git commit, git branch, and git switch (or git checkout) without looking them up. You should know that a commit is a snapshot with a parent and a unique hash, and you should be able to read git log and git status output in a terminal.
Why this matters for stash, cherry-pick, and bisect
Each of the three commands in this tutorial reads or writes a different combination of three places. Most surprises with them come from not knowing which place a change is in. This chapter gives you the model you will use in every later chapter.
The working tree, the index, and the commits
Git tracks your changes in three places:
- Working tree: the files on disk that you edit. Git does not record anything here until you ask it to.
- Index (also called the staging area): a proposed next commit.
git addcopies the current content of a file from the working tree into the index. - Commits: permanent snapshots in the repository.
git committurns the index into a new commit and moves the current branch to point at it.
HEAD is the pointer to the commit you currently have checked out. Git compares the three places to produce the output of git status.
Reading git status as a comparison
git status shows two comparisons:
- Index against HEAD: listed under "Changes to be committed". These changes are staged.
- Working tree against index: listed under "Changes not staged for commit". These are edited but not staged.
Files Git has never tracked appear separately under "Untracked files". This detail matters later, because git stash ignores untracked files unless you ask otherwise.
A worked example
Create a small repository and put a file in each state:
git init demo && cd demo
echo "one" > a.txt
echo "one" > b.txt
git add a.txt b.txt
git commit -m "add a and b"
echo "two" >> a.txt # edited, not staged
echo "two" >> b.txt
git add b.txt # edited and staged
echo "new" > c.txt # untracked
git status --shortThe short status output is:
M a.txt
M b.txt
?? c.txtThe two columns before each file name are the index state (left) and the working tree state (right). M means modified in the working tree only. M means modified and staged. ?? means untracked.
Now edit b.txt again after staging it:
echo "three" >> b.txt
git status --shortYou will see MM b.txt. The file has a staged version (with "two") and a different working tree version (with "three"). The index holds a snapshot of the file at the moment you ran git add, not a link to the file. To see each difference, use:
git diff # working tree against index
git diff --staged # index against HEAD
git diff HEAD # working tree against HEADHow the three commands touch these places
Each command in this tutorial works on a different combination of the places:
| Command | Reads | Writes |
|---|---|---|
| `git stash` | Working tree and index (tracked files) | A new stash commit; then resets the working tree and index to match HEAD |
| `git cherry-pick` | One existing commit | A new commit on the current branch, plus the index and working tree |
| `git bisect` | Commits in history | Your checked-out working tree, moved to a series of older commits |
Keep these differences in mind:
git stashis the only one of the three that starts from uncommitted changes. It removes them from your working tree and index and stores them for later.git cherry-pickstarts from a commit and needs a clean enough working tree to apply it. Uncommitted changes in the files it touches can block it.git bisectchecks out different commits, so uncommitted changes can block a checkout or get in the way of testing. You will often stash first.
Common mistakes
- Assuming
git addis final. Staging captures a file at one moment. Edit the file again and you need to rungit addagain. - Expecting untracked files to be safe or saved. A new file such as
c.txtabove is not in HEAD or the index. Commands that save or restore tracked changes do not include it by default. - Confusing the index with the last commit.
git diffshows nothing for a staged change, which can look like the change is gone. Usegit diff --stagedto see it.
Exercise
You run git status --short and see MM notes.txt. What does this tell you, and which command shows only the part that a git commit would record?
Show answer
The file has a staged change (left column) and a further unstaged change in the working tree (right column). git diff --staged shows the staged part, which is what git commit would record.