safe-git-worktree

A set of rules for protecting uncommitted work and the checked-out Git working tree during branch or pull-request tasks. Git is a tool for tracking code changes, and a worktree is an isolated directory for another branch.

In plain words
What is it for?
Check repository state, fetch the current remote base, preserve local changes, isolate risky work, and keep changes on feature branches.
Why use it?
It reduces the risk of overwriting local edits, using an outdated base branch, or changing shared branch history.

Cursor rule for Cursor

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/caipe-io/ai-platform-engineering/safe-git-worktree
Clone the repo
git clone --depth 1 https://github.com/caipe-io/ai-platform-engineering

Made for: Cursor.

Per session 877 This file is loaded in full into every session.
When invoked 877 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00877 $0.00877
Opus 5 $0.00439 $0.00439
Sonnet 5 $0.00175 $0.00175
Haiku 4.5 $0.00088 $0.00088

Measured 2d ago against content hash 4d122f22df69, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

safe-git-worktree scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.cursor/rules/safe-git-worktree.mdc · 80 lines

How it starts

The opening of the file, as written. The whole thing — 80 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Safe Git: Protect the User's Working Tree

The user's checked-out branch and working tree are theirs. Before doing any branch/PR work, make sure you will not lose their edits or churn thousands of files. The failure that motivated this rule was switching branches across a stale local main (37 commits behind origin/main), which rewrote ~3,800 docs/ files in the user's tree.

Always (universal — applies to every repo and agent)

  • Never assume local main is current. Run git fetch origin <base> and branch from the remote ref (origin/main), not the local one.
  • Preserve uncommitted work. Run git status before any git command. If the tree is dirty, do not switch/reset/checkout over those edits — commit, stash with the user's awareness, or isolate in a worktree instead.
  • Never push to a shared branch. Push only to feature branches; never push/--force to origin/main (or any protected branch).
  • Never rewrite shared history (reset --hard, force-push) on a branch others may have pulled.
  • Branch naming: use the prebuild/ prefix when the branch should trigger CI Docker image builds (see AGENTS.md).

Prefer an isolated worktree when the work is risky or parallel

Reach for a throwaway worktree when you need to experiment, run work in parallel with the user's current branch, or when the tree is dirty and you must not disturb it:

git fetch origin main
git worktree add /tmp/caipe-<task> origin/main -b prebuild/<feat-branch>
cd /tmp/caipe-<task>          # do edits, commits, pushes here
# when done:
cd -                          # user's repo, untouched
git worktree remove /tmp/caipe-<task>

Worktree caveats to handle (don't assume the worktree "just works"):

  • Untracked-but-essential files don't come along. Gitignored config such as .env is absent in a fresh worktree — copy or symlink what the task needs.
  • Virtualenvs are not shared. Per CLAUDE.md, each worktree (and each subpackage: RAG ingestors/server, MCP agents) needs its own uv venv --python python3.13 --clear .venv && uv sync.
  • /tmp is cleared on reboot and duplicates large caches — fine for short tasks, not for long-lived state.

Read the full file on GitHub · 80 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 80 lines · 877 tokens per session scan A 4d122f22df69

Subscribe to this mod's changes

safe-git-worktree is a cursor rule published in the GitHub repository caipe-io/ai-platform-engineering (403 stars, last pushed 3d ago), licensed Apache-2.0. It adds 877 tokens to every session, about $0.0044 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.