Skip to content

hookGit and releases

Inject Git Branch

A Claude Code hook that adds the current Git branch and a count of uncommitted files to every prompt you send, so the agent always knows where it is.

npx workspacetemplates add inject-git-branch --tool claude-code

Writes 1 file to .claude/

.claude/settings.jsonmerge into your file15 lines · 792 BFile
{
  "hooks": {
    "UserPromptSubmit": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "node -e \"let d='';process.stdin.on('data',c=>d+=c).on('end',()=>{let j={};try{j=JSON.parse(d)}catch(e){}const cp=require('child_process');const g=a=>cp.execFileSync('git',a,{cwd:j.cwd||process.cwd(),encoding:'utf8',stdio:['ignore','pipe','ignore']});try{const b=g(['branch','--show-current']).trim()||('detached at '+g(['rev-parse','--short','HEAD']).trim());const n=g(['status','--porcelain']).split(String.fromCharCode(10)).filter(Boolean).length;console.log('Git context: branch '+b+', '+(n?n+' uncommitted file'+(n>1?'s':''):'working tree clean')+'.')}catch(e){}process.exit(0)})\"",
            "timeout": 10
          }
        ]
      }
    ]
  }
}

What it does

Long sessions drift: you switch branches in another terminal and the agent keeps assuming the old one. This UserPromptSubmit hook runs two quick Git queries before each prompt and prints one line that Claude Code adds to the context.

The line names the branch, or the short commit when HEAD is detached, and says how many files are modified or untracked. Outside a Git repository it prints nothing.

Example

you › Write a commit message for what I have staged.

agent › Claude sees 'Git context: branch fix/login-redirect, 3 uncommitted files.' with the prompt and can mention the branch when it drafts the message.

Questions

Does it slow each prompt down?
Barely. Two local Git calls finish in a few milliseconds, and nothing touches the network.
What does it print on a detached HEAD?
It names the short commit hash instead of a branch, for example 'detached at 4f2a9c1'.
  • Block Git No Verify

    HookGit and releases

    Free
  • Block Git Reset Hard

    HookGit and releases

    Free
  • Detect Merge Markers

    HookGit and releases

    Free
  • Session Git Context

    HookGit and releases

    Free