Created
August 14, 2026 05:19
-
-
Save fadhlirahim/901fc0f5a33c229dc9cf453325d032a8 to your computer and use it in GitHub Desktop.
When you have a directory created by a git worktree and you start the claude session on that directory, Claude code agent would not have memory of the main branch. Place this file inside ~/.claude/hooks/ and then update your ~/.claude/settings.json to use this
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # SessionStart: point a git worktree's auto-memory dir at its main repo's. | |
| # | |
| # Auto-memory lives at ~/.claude/projects/<sanitized-cwd>/memory/, and a worktree | |
| # has a different cwd than the repo it belongs to, so a session started in one | |
| # begins blind to every memory written from the main checkout. This symlinks the | |
| # worktree's memory dir onto the main repo's so both read and write one set. | |
| # | |
| # Bails out (exit 0, silent) unless it is safe: not a git repo, not a worktree, | |
| # main repo has no memory dir yet, already linked, or the worktree dir already | |
| # holds its own content. | |
| set -uo pipefail | |
| payload=$(cat 2>/dev/null || true) | |
| cwd=$(printf '%s' "$payload" | jq -r '.cwd // empty' 2>/dev/null) | |
| [ -n "$cwd" ] && [ -d "$cwd" ] || cwd=$(pwd) | |
| cwd=$(cd "$cwd" 2>/dev/null && pwd -P) || exit 0 | |
| # --git-common-dir points at the ORIGINAL repo's .git for a worktree, and at the | |
| # local .git otherwise, so its parent identifies the main checkout either way. | |
| common=$(git -C "$cwd" rev-parse --git-common-dir 2>/dev/null) || exit 0 | |
| [ -n "$common" ] || exit 0 | |
| case "$common" in /*) ;; *) common="$cwd/$common" ;; esac | |
| main=$(cd "$common/.." 2>/dev/null && pwd -P) || exit 0 | |
| [ "$main" = "$cwd" ] && exit 0 | |
| # Same sanitizing the harness applies to cwd: every non-alphanumeric becomes "-". | |
| slug() { printf '%s' "$1" | tr -c 'A-Za-z0-9-' '-'; } | |
| root="$HOME/.claude/projects" | |
| target="$root/$(slug "$main")/memory" | |
| mine="$root/$(slug "$cwd")/memory" | |
| # Create the main repo's dir if it has none yet, rather than bailing: otherwise | |
| # the first worktree of a fresh repo writes strays with nothing to link to. | |
| mkdir -p "$target" 2>/dev/null || exit 0 | |
| [ "$mine" = "$target" ] && exit 0 | |
| [ -L "$mine" ] && exit 0 | |
| if [ -e "$mine" ] && [ -n "$(ls -A "$mine" 2>/dev/null)" ]; then | |
| printf '{"systemMessage":"auto-memory: %s already has its own files; not linking. Merge it into the main repo manually."}\n' "$mine" | |
| exit 0 | |
| fi | |
| rmdir "$mine" 2>/dev/null | |
| mkdir -p "$(dirname "$mine")" 2>/dev/null || exit 0 | |
| if ln -s "$target" "$mine" 2>/dev/null; then | |
| printf '{"systemMessage":"auto-memory linked to main repo: %s"}\n' "$(basename "$main")" | |
| fi | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment