status, log, and diff
git status, git log, git diff
After this page you can read the three-tree report, walk history, and see a patch before you commit it.
git status — where you are
cd ~/desk/desk-api
git status
git status -sb # short: branch + one letter per file-sb prints ## main and lines like M README.md (modified) or ?? notes.txt (untracked). Use it when the long form is noise.
git status is the three-tree report from chapter 1: working tree vs index vs HEAD.
git diff git diff --staged git log
working vs index index vs HEAD HEAD chain
working tree index HEAD
┌──────────┐ ┌──────────┐ A ← B ← C main
│ extra │ │ staged │
└──────────┘ └──────────┘
git log — the DAG as a list
git log
git log --oneline
git log --oneline --graph --decorate --all
git log -3 # last three
git log --oneline -- README.md # commits that touched this path
git log -p -1 # last commit plus its patch--oneline is the default view in this book. --graph --decorate --all is the view that matches the mental-model diagrams.
Useful format:
git log --format='%h %an %s'%h short hash, %an author name, %s subject. git help log lists more placeholders. You will use --format in hooks and CI later; for now, --oneline is enough.
git diff — the patch
git diff # working tree vs index (unstaged)
git diff --staged # index vs HEAD (what the next commit will contain)
git diff HEAD # working tree vs HEAD (staged and unstaged together)
git diff HEAD~1 HEAD # last commit vs its parent
git diff main feature # two branches (once you have them, chapter 8)Empty git diff and non-empty git diff --staged means everything is already in the index. Empty both, plus an untracked file in status, means you still need git add.
DiffTool
For a side-by-side view in the terminal:
git config --global diff.tool nvimdiff # or vimdiff, meld
git difftool README.md
git difftool --stageddifftool opens each changed file in the tool you named. mergetool is the sibling for conflicts (chapter 8). Stay on git diff until you want the split view. The patch is the same data.
A one-minute drill
echo "note" >> README.md
git status
git diff
git add README.md
git diff # empty
git diff --staged # the note
git restore --staged README.md
git restore README.md # discard the note
git status # cleanrestore is the next page, on purpose. Here you only need to see the three diffs change as the file moves.