Using Git Commits as Claude Code's Memory

Published: (April 21, 2026 at 05:47 AM EDT)
3 min read
Source: Dev.to

Source: Dev.to

The problem

Claude Code’s context window is a liability. The longer a session runs, the more you pay to re‑read your own conversation history, and the more likely the model drifts on earlier context.

  • Context accumulates fast on long tasks.
  • Earlier decisions get buried.
  • Token costs climb.
  • /clear wipes everything, including useful state.
  • Switching between tasks in the same session bleeds context.

Most people either let sessions balloon or /clear and lose everything.

Solution: Git as a memory layer

Treat Git commit messages as free, durable, searchable context. A well‑written commit body becomes a session summary that survives /clear, costs nothing to store, and integrates naturally into your workflow.

1. One task → one worktree → one branch

git worktree add ../task-foo -b task/foo

Each task gets its own branch and working directory, preventing context bleeding. Open Claude Code inside that worktree.

2. Commit‑body session summary

Use a rich commit format at the end of each session:

feat: add user auth flow

what: JWT‑based auth with refresh token rotation
why: existing session cookie approach didn't work cross‑subdomain
tried: httpOnly cookie sharing — blocked by Safari ITP
next: wire up logout endpoint (separate task)

The tried and next fields capture reasoning that doesn’t live in the code. You don’t write this manually—tell Claude Code to commit, and it follows the format automatically (see the “Commit format” section below).

3. Session start orientation

When you open Claude Code in the worktree, it automatically runs:

git log -5 --format="%s%n%b"

This reads recent commit messages and orients the model before any questions are asked.

Handling special cases

  • Exploratory sessions with no commits – create a NOTES.md or SCRATCH.md file, make a single commit whose body is the session summary.
  • Nuanced information – keep a CONTEXT.md in the worktree, add it to .gitignore so it never enters the repository; it lives locally and disappears when the worktree is removed.
  • Multi‑session tasks – each session adds its own commit; the series of commit bodies forms a running log.

Commit format (must‑have)

type: short summary

what: what changed
why: motivation
tried: dead ends / rejected approaches
next: follow‑up tasks (if any)

Full workflow diagram

git worktree add ../task-name -b task/name

Open Claude Code in that worktree
  → Claude reads `git log`, orients automatically

Work on the task

Tell Claude Code: "commit"
  → Claude writes the rich commit message, you review and confirm

/clear  (or close the session)

git worktree remove ../task-name  (when merged)

Benefits

  • Token efficient – fresh sessions stay small.
  • Durable – context survives /clear and machine restarts.
  • Searchablegit log --grep="auth" finds past decisions.
  • Discipline‑forcing – writing the commit forces you to articulate what you did.
  • Zero overhead tooling – it’s just Git.

Closing thought

Treat the commit message as a briefing document for your next Claude Code session, not merely a changelog entry.

Have a different approach to managing Claude Code context? I’d love to hear it.

0 views
Back to Blog

Related posts

Read more »