Inject git status into every Claude Code session with a SessionStart hook
Claude Code can run a hook whenever a session starts, which makes it the natural place to inject context you would otherwise retype in every chat: current branch, dirty files, open TODOs. The post shows a compact SessionStart command, where to put it, and why capping its output matters for token cost and noise.
Key points
- A SessionStart hook runs once at session start and its stdout is appended to the session context.
- The hook is declared under "hooks" in .claude/settings.json with type: command.
- Branch comes from git rev-parse --abbrev-ref HEAD, changes from git status -sb.
- Cap output with head -40, otherwise a large status dump is added to every conversation.
- Keep personal hooks in settings.local.json so they stay out of version control.
- Stable conventions belong in CLAUDE.md; the hook is only for live facts.
A SessionStart hook runs once when Claude Code opens, and whatever the command prints to stdout is added to the session context. The example registers it under "hooks" in .claude/settings.json as a command that reads the branch with git rev-parse and prints a trimmed git status -sb, so the model starts out knowing which branch is checked out and which files are dirty.
Two practical constraints come with it: cap the output, since a full status dump lands in every conversation and eats context, and use settings.local.json when the hook is personal and should not be committed. Keep git calls fail-soft so a folder that is not a repository does not break startup, and leave stable conventions in CLAUDE.md rather than in the hook.
Real-world use
- Teams working across many branches that want every Claude Code session to start with the right branch and working-tree state.
- A developer in a long-lived repo who no longer has to type "which branch am I on, what changed" at the start of each chat.
- A monorepo where the hook prints only the status of the subproject being worked on.
- Reducing accidental overwrites by making the agent aware of uncommitted changes from the first message.
Why it matters
Injecting live context automatically instead of retyping it gives the agent accurate starting information and removes repetitive prompting; the only real cost is remembering to keep the hook's output short.