Sub topic 6
Reading git status
This chapter assumes you have Git installed, can use a terminal, and have already run git init in a project directory. You should also know that Git tracks changes in three places: the working tree, the staging area, and the repository.
What git status reports
git status shows where each change sits among those three places. It never changes anything. Run it often, especially before git add and git commit.
The output has up to three sections:
| Section | Meaning |
|---|---|
| Changes to be committed | Staged changes that the next commit will include |
| Changes not staged for commit | Modified tracked files that are not staged yet |
| Untracked files | New files that Git has never been told to track |
A worked walkthrough
Start in a fresh repository and create one file.
mkdir status-demo
cd status-demo
git init
echo "# Status Demo" > README.md
git statusGit prints something like this:
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
nothing added to commit but untracked files present (use "git add" to track)Three things stand out. The branch name is main (older setups may show master). The line "No commits yet" means the history is empty. README.md is untracked, so Git sees it but will not record it until you stage it.
Now stage the file and check again.
git add README.md
git statusChanges to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.mdThe file moved from untracked to staged. After you commit, edit the file again:
git commit -m "Add README"
echo "More details." >> README.md
git statusChanges not staged for commit:
modified: README.mdThe file is now tracked, and its working copy differs from the last commit.
A file can appear twice
A file can be both staged and modified. If you stage a file and then edit it again, git status lists it under "Changes to be committed" and under "Changes not staged for commit". The staged copy is the version at the time you ran git add. Your later edit is only in the working tree. Committing now records the staged version and leaves the newer edit behind. Run git add again to include it.
Shorter output
For a compact view, use the short format.
git status -sEach line has two status letters followed by the file name. The first letter describes the staging area and the second describes the working tree. For example, A means newly staged, M means modified but unstaged, MM means staged and then modified again, and ?? means untracked.
Common mistakes
- Reading "nothing to commit, working tree clean" as "nothing exists". It means every tracked file matches the last commit. Ignored files do not appear at all.
- Expecting a new empty directory to show up. Git tracks files, not directories, so an empty directory is invisible to
git status. - Forgetting that an untracked file is not in any commit, even after you commit other files.
Exercise
You run git add notes.txt, then edit notes.txt again, then run git status -s. What two-character code do you expect for notes.txt, and what will git commit record?
Show answer
You expect MM: the first M means staged changes exist, and the second M means further unstaged changes exist. git commit records only the staged version from the time of git add. The later edit stays in the working tree until you run git add again.