HookStack
Back to catalogue
ContextSessionStart

Load Git context at startup

Starts every session knowing your repo state

At session start, injects the repository state: current branch, git status, latest commits. The agent starts with a clear view of the project state.

What does the Load Git context at startup hook do?

Load Git context at startup is a Claude Code SessionStart hook. It fires automatically at that lifecycle event — outside the model, so it can't be skipped or forgotten. Starts every session knowing your repo state.

As a SessionStart hook it runs after the action, reacting to what just happened rather than blocking it. Because it is a deterministic Node.js script, it executes on every matching event without relying on the model to remember — the guarantee that makes agentic workflows safe to automate.

Use cases

  • Context resumption
  • Git awareness
  • Multiple sessions

Tags

#context#git#session#awareness

settings.json fragment

{
  "hooks": {
    "SessionStart": [
      {
        "hooks": [
          {
            "command": "node $CLAUDE_PROJECT_DIR/.claude/hooks/load-git-context.mjs",
            "type": "command"
          }
        ]
      }
    ]
  }
}

Script · .claude/hooks/load-git-context.mjs

#!/usr/bin/env node
// @hookstack session-start-load-git-context
// Injecte le contexte git (branche, statut) dans chaque prompt (UserPromptSubmit)
import { execSync } from "node:child_process";
import { fileURLToPath } from "node:url";

function defaultExec(cmd) {
	try {
		return execSync(cmd, { encoding: "utf8", timeout: 5_000 }).trim();
	} catch {
		return "";
	}
}

export function run({ exec = defaultExec } = {}) {
	const branch =
		exec("git branch --show-current") ||
		exec("git rev-parse --abbrev-ref HEAD");
	const commit = exec('git log -1 --pretty="%h %s"');
	const status = exec("git status --short");

	if (!branch) return null;

	const lines = ["## Contexte Git", `- Branche : \`${branch}\``];
	if (commit) lines.push(`- Dernier commit : ${commit}`);
	if (status)
		lines.push(
			`- Fichiers modifiés :\n${status
				.split("\n")
				.map((l) => `  ${l}`)
				.join("\n")}`,
		);
	else lines.push("- Répertoire de travail propre");

	return `${lines.join("\n")}\n`;
}

/* v8 ignore next 4 */
if (process.argv[1] === fileURLToPath(import.meta.url)) {
	const result = run();
	if (result) process.stdout.write(result);
}

Learn more

Related hooks