Skip to content
Back to skills

Worktrees And Branches

ASecurity

A separate Git worktree for every task, created from the repository's main clone: the worktree folder name, creating the task branch from the main branch, continuing from a task branch that an earlier session pushed, the branch name format and regex, and removing the worktree and branch after merge. Load before touching code, when creating a branch, and during post-merge cleanup.

  • 3 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added December 2, 2026
developmentshellbashgit

Security analysis

A100/100

Scanned December 2, 2026

npx -y skills add the-up/ai --skill worktrees-and-branches --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Worktrees And Branches?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Worktrees And Branches
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/the-up-worktrees-and-branches/badge)](https://www.skillsdirectory.com/skills/the-up-worktrees-and-branches)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: worktrees-and-branches
description: "A separate Git worktree for every task, created from the repository's main clone: the worktree folder name, creating the task branch from the main branch, continuing from a task branch that an earlier session pushed, the branch name format and regex, and removing the worktree and branch after merge. Load before touching code, when creating a branch, and during post-merge cleanup."
---

# Worktrees and Branches

- Develop every task in its own Git worktree; don't work in the main clone. Only you work in a task's worktree; don't share it with other sessions.
- Create the worktrees from the repository's main clone: the clone of the repository in a folder named `<repo>` (check its `origin` URL). If there's none, clone the repository once into the session's root folder as `<repo>`. In the main clone, run only the `fetch`, `worktree`, and `branch` commands in these rules; leave its checked-out branch and local changes as they are, and don't delete it, because other sessions and their worktrees depend on it.
- Determine the main branch from the remote repository. Fetch it in the main clone, then add the worktree next to the main clone as `<repo>-<TASK-KEY>`, or as `<repo>-<slug>` when there's no task, on a new task branch that starts from the remote main branch:

  ```bash
  git -C <repo> fetch origin
  ```

  ```bash
  git -C <repo> worktree add --no-track -b <branch> ../<repo>-<TASK-KEY> origin/<main-branch>
  ```

  With `--no-track`, the task branch doesn't track the main branch; set its upstream on the first push with `git push -u origin <branch>`.

- If the worktree for this task already exists (`git worktree list`), use it instead of adding another one.
- When the task branch already exists on the remote, for example because an earlier session pushed work in progress, as the [Task Management](../task-management/SKILL.md) rules describe, or opened its PR, fetch in the main clone and add the worktree from that branch instead of a new branch from the main branch, so that the work in progress comes with it:

  ```bash
  git -C <repo> fetch origin
  ```

  ```bash
  git -C <repo> worktree add ../<repo>-<TASK-KEY> <branch>
  ```

  When the main clone has no local branch of that name, Git creates it from `origin/<branch>` and sets it to track that branch. When the worktree or a local branch for the task already exists on this computer and the task branch has moved on the remote, bring it up to date with `git -C <repo>-<TASK-KEY> pull --ff-only` before you continue; when it can't fast-forward, don't force anything, and ask the user.

- A new worktree contains only tracked files: generate `.env` in it according to the [Environment Variables](../environment-variables/SKILL.md) rules, and install the dependencies before you start work in it.
- Commit only to the task branch; don't commit directly to the main branch or a protected branch. Git checks out a branch in only one worktree at a time, so don't switch the task worktree to the main branch.
- All worktrees share one stash. To set work aside, commit it to the task branch instead of using `git stash`, so that another session doesn't apply or drop it.
- Name the branch `feature/<TASK-KEY>-<slug>`, or `feature/<slug>` when there's no task; the slug is short, lowercase kebab-case. The name must match this regex:

  ```text
  ^feature/([A-Z][A-Z0-9]+-[0-9]+-)?[a-z0-9]+(-[a-z0-9]+)*$
  ```

  Example: `feature/PROJ-123-prevent-duplicate-payments`

- As the last step of the post-merge cleanup, remove the task worktree from the main clone, then delete the task branch; Git doesn't delete a branch that a worktree has checked out:

  ```bash
  git -C <repo> worktree remove ../<repo>-<TASK-KEY>
  ```

  ```bash
  git -C <repo> branch -D <branch>
  ```

  Before removing the worktree, confirm that the PR is merged and that the worktree has no uncommitted changes, untracked files, commits that aren't in the PR, or stash entries for the task branch. If anything remains, don't remove it, don't use `--force`, and ask the user.

  Leave the worktree before you remove it, so that no shell or session has it as its working directory: on Windows, Git can't delete a folder that a process uses that way, and `git worktree remove` fails and leaves the empty folder behind.

- Git doesn't recommend checking out a repository with submodules in several worktrees. For such a repository, work in a separate clone with the same folder name instead (`git clone --branch <main-branch> <repo-url> <repo>-<TASK-KEY>`), and delete it at the end of the post-merge cleanup after the same checks.
- For a review that the user asks for, as the [Core Rules](../core-rules/SKILL.md) describe, add a worktree without a task branch: fetch in the main clone, then check out the change's latest commit next to the main clone as `<repo>-<slug>`, and remove the worktree when the review is done:

  ```bash
  git -C <repo> fetch origin
  ```

  ```bash
  git -C <repo> worktree add --detach ../<repo>-<slug> origin/<branch>
  ```

  ```bash
  git -C <repo> worktree remove ../<repo>-<slug>
  ```

  It doesn't wait for a merge and has no branch to delete. Leave it before you remove it, as you would a task worktree. Nothing should be written in it, so if `git worktree remove` refuses because the worktree has changes, don't use `--force`, and ask the user.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.