New: SEO Blog Writer, a readable, checked blog post for your site every week. Pay once, download straight away.

See it
AI Skill Up
Browse skills

Show the git branch in your Claude Code status line

Knowing which branch a session is on prevents commits to the wrong one. How to read the folder from the status line JSON, get the branch fast, and stay quick.

With two or three Claude Code sessions open, the question you ask most is which one is which. The folder and the branch answer it, and they belong on the status line, where you see them before every prompt.

Get the folder from the JSON

input=$(cat)
dir=$(echo "$input" | jq -r '.workspace.current_dir // empty')
name=$(basename "$dir")

Read it from the input rather than using pwd. The status line JSON is the session's view of where it is, which is what you want to see.

Get the branch, cheaply

branch=$(git -C "$dir" --no-optional-locks branch --show-current 2>/dev/null)
[ -z "$branch" ] && branch=$(git -C "$dir" --no-optional-locks rev-parse --short HEAD 2>/dev/null)

branch --show-current is fast and prints nothing on a detached HEAD, so the second line falls back to the short commit. Outside a repository both print nothing and the script moves on. --no-optional-locks stops the status line from taking locks while Claude may be running git itself.

Keep it fast

The status line runs after each reply, and Claude Code cancels a run that is still going when the next update arrives. A script that calls git status on a big repository can simply never finish. If you want a dirty marker, keep it cheap:

dirty=""
git -C "$dir" --no-optional-locks diff --quiet 2>/dev/null || dirty="*"

Where it goes on the line

Left, and always visible. When the terminal is narrow, drop other detail before the folder. Two sessions in two worktrees look identical otherwise; see running several Claude Code sessions. The width rules are in the Claude Code status line.

Capsule Status Line keeps your folder and branch on the left at every width, beside capsules for context and both usage limits, and drops detail in a fixed order in a narrow terminal so the folder is the last thing to go.

Questions

How does a status line script know the current folder?

Claude Code passes it in the JSON on standard input, as workspace.current_dir. Read it from there rather than relying on the script's working directory.

Why is my status line slow with git in it?

git status on a large repository takes time, and the status line runs after every reply. Use the cheapest command that answers the question, such as branch --show-current, and add --no-optional-locks.

What does --no-optional-locks do?

It stops git taking optional locks, such as the index refresh that status performs. That avoids a status line fighting with a git command Claude is running at the same moment.

Or skip the afternoonand drop one in.