| name | git-worktree-ops |
| description | Expert guidance for Git worktree operations, safe branching, and multi-agent workflow management. Use for creating/removing worktrees, managing branches, troubleshooting conflicts, and ensuring guardrails compliance. Also covers standard Git operations like committing, merging, rebasing, stashing, and conflict resolution. |
Git Worktree Operations
Expert skill for safe Git operations in multi-agent worktree environments and standard Git workflows.
When to Use This Skill
Worktree Operations:
- Creating or removing Git worktrees
- Managing branches across multiple worktrees
- Troubleshooting worktree-related issues
- Verifying guardrails are properly configured
- Cleaning up stale worktrees
- Understanding git worktree metadata
Standard Git Operations:
- Creating, switching, and managing branches
- Committing and staging changes
- Merging and rebasing branches
- Resolving conflicts
- Working with remote repositories
- Viewing history and diffs
- Undoing changes safely
Core Worktree Commands
Create Worktree
Basic pattern:
git worktree add -b <new-branch> <path> <base-branch>
Examples:
git worktree add -b feat/codex-fix-auth ../myapp-worktrees/codex-fix-auth develop
git worktree add -b feat/new-feature ../myapp-worktrees/new-feature HEAD
git worktree add ../myapp-worktrees/existing-work feat/existing-branch
Naming conventions (for this project):
- Branch:
feat/<agent>-<task> (e.g., feat/codex-fix-auth)
- Worktree path:
<agent>-<task> (matches branch without feat/)
List Worktrees
git worktree list
git worktree list --verbose
git worktree list --porcelain
Remove Worktree
git worktree remove <path>
git worktree remove --force <path>
git worktree remove <path>
git branch -D <branch-name>
Prune Stale Metadata
git worktree prune
git worktree prune --dry-run
git worktree prune --verbose
Guardrails Configuration
Set Up Branch/Worktree Binding
Required before starting any agent in a worktree:
cd ../myapp-worktrees/codex-fix-auth
git branch --show-current
git rev-parse --show-toplevel
GIT_DIR="$(git rev-parse --git-dir)"
BRANCH="feat/codex-fix-auth"
WORKTREE="$(pwd -P)"
printf '%s\n' "$BRANCH" > "$GIT_DIR/agent-expected-branch"
printf '%s\n' "$WORKTREE" > "$GIT_DIR/agent-expected-worktree"
cat "$GIT_DIR/agent-expected-branch"
cat "$GIT_DIR/agent-expected-worktree"
Verify Guardrails Active
test -x "$(git rev-parse --git-path hooks/pre-commit)" && echo "pre-commit: ✓" || echo "pre-commit: ✗"
test -x "$(git rev-parse --git-path hooks/pre-push)" && echo "pre-push: ✓" || echo "pre-push: ✗"
test -f "$(git rev-parse --git-dir)/agent-expected-branch" && echo "branch binding: ✓" || echo "branch binding: ✗"
test -f "$(git rev-parse --git-dir)/agent-expected-worktree" && echo "worktree binding: ✓" || echo "worktree binding: ✗"
cat "$(git rev-parse --git-path hooks/pre-commit)"
Safe Branch Operations
Allowed Operations
git fetch origin
git rebase origin/develop
git fetch origin
git merge origin/develop
git push origin feat/codex-fix-auth
git pull --ff-only
git add .
git commit -m "feat: implement authentication retry logic"
Forbidden Operations
git rebase feat/claude-add-metrics
git push --force origin feat/codex-fix-auth
git push --force-with-lease origin feat/codex-fix-auth
git push origin feat/codex-fix-auth:develop
git push origin :feat/codex-fix-auth
git push origin --delete feat/codex-fix-auth
Conflict Resolution
When Two Agents Touch Same Files
Strategy: Merge one branch first, then rebase the second.
cd ~/src/myapp
git checkout develop
git pull --ff-only
git merge --no-ff feat/codex-fix-auth
git push origin develop
cd ../myapp-worktrees/claude-add-metrics
git fetch origin
git rebase origin/develop
git add .
git rebase --continue
npm test
cd ~/src/myapp
git checkout develop
git pull --ff-only
git merge --no-ff feat/claude-add-metrics
git push origin develop
Important: Never ask agents to auto-resolve conflicts between agent branches without human oversight.
Abort Rebase if Needed
git rebase --abort
git status
git log --oneline --graph --all --decorate -10
Standard Git Branch Operations
For teams not using worktrees or for single-developer scenarios, these standard Git operations provide safe workflows.
Branch Management
Create and switch to new branch:
git checkout -b feat/new-feature
git checkout -b feat/new-feature develop
git switch -c feat/new-feature
git switch -c feat/new-feature develop
Switch between branches:
git checkout develop
git switch develop
git checkout -
git switch -
git checkout -f develop
List branches:
git branch
git branch -v
git branch -vv
git branch -a
git branch -avv
git branch -r
git branch --merged
git branch --no-merged
git branch --list '*fix*'
git branch -a --list '*feature*'
Delete branches:
git branch -d feat/completed-feature
git branch -D feat/abandoned-feature
git push origin --delete feat/old-feature
git push origin :feat/old-feature
git fetch --prune
Rename branch:
git branch -m new-name
git branch -m old-name new-name
git push origin :old-name new-name
git push origin -u new-name
Staging and Committing
Stage changes:
git add file1.js file2.js
git add src/
git add .
git add --all
git add -u
git add -p
git add -i
git add -p file.js
Unstage changes:
git restore --staged file.js
git reset HEAD file.js
git restore --staged .
git reset HEAD
Commit changes:
git commit -m "feat: add user authentication"
git commit
git commit -am "fix: resolve login timeout"
git commit --amend
git commit --amend -m "Updated commit message"
git commit --author="Name <email>" -m "Message"
git commit --allow-empty -m "Trigger CI"
Conventional commit format:
git commit -m "feat: add new feature"
git commit -m "fix: resolve bug"
git commit -m "docs: update README"
git commit -m "style: format code"
git commit -m "refactor: restructure"
git commit -m "test: add tests"
git commit -m "chore: update deps"
Viewing Changes and History
Status and diffs:
git status
git status -s
git status -sb
git diff
git diff --staged
git diff --cached
git diff file.js
git diff --staged file.js
git diff develop..feat/new-feature
git diff develop...feat/new-feature
git diff --word-diff
git diff --stat
git diff --shortstat
View history:
git log
git log --oneline
git log --oneline --graph --all --decorate
git log -5
git log --oneline -10
git log --author="John"
git log --since="2 weeks ago"
git log --after="2024-01-01" --before="2024-02-01"
git log -- file.js
git log -p -- file.js
git log --grep="fix"
git log --grep="auth" --grep="login" --all-match
git log -S "function name"
git log -G "regex pattern"
git log --pretty=format:"%h - %an, %ar : %s"
git log --graph --pretty=format:"%C(yellow)%h%Creset %C(blue)%an%Creset %s"
Show specific commit:
git show <commit-hash>
git show HEAD
git show HEAD~1
git show HEAD~3
git show <commit-hash>:path/to/file.js
git show --stat <commit-hash>
Merging
Fast-forward merge:
git merge feat/new-feature
git merge --ff-only feat/new-feature
No fast-forward merge (creates merge commit):
git merge --no-ff feat/new-feature
git merge --no-ff -m "Merge feature X" feat/new-feature
Squash merge (combine all commits):
git merge --squash feat/new-feature
git commit -m "Add feature X"
Abort merge:
git merge --abort
git reset --hard HEAD
Rebasing
Interactive rebase:
git rebase -i HEAD~3
git rebase develop
git rebase -i develop
Continue/abort rebase:
git add .
git rebase --continue
git rebase --skip
git rebase --abort
Rebase vs Merge:
git rebase develop
git merge develop
Conflict Resolution
When conflicts occur:
git status
cat file.js
vim file.js
git add file.js
git commit
git rebase --continue
Conflict resolution tools:
git mergetool
git checkout --theirs .
git add .
git checkout --ours .
git add .
git checkout --theirs path/to/file.js
git add path/to/file.js
View conflict diff:
git diff
git diff --name-only --diff-filter=U
git log --merge -p
Undoing Changes
Discard uncommitted changes:
git restore file.js
git checkout -- file.js
git restore .
git checkout -- .
git restore --staged --worktree .
git reset --hard HEAD
Undo commits:
git reset --soft HEAD~1
git reset HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1
git reset --hard HEAD~3
git reset --hard <commit-hash>
Revert commits (safe for shared branches):
git revert <commit-hash>
git revert HEAD
git revert HEAD~3..HEAD
git revert -n <commit-hash>
git revert --no-commit <commit-hash>
Recovery:
git reflog
git reflog
git reset --hard <commit-hash-from-reflog>
git reflog
git checkout -b recovered-branch <commit-hash>
git fsck --lost-found
Remote Operations
Fetch updates:
git fetch --all
git fetch origin
git fetch --all --prune
git fetch origin develop
Pull changes:
git pull
git pull --ff-only
git pull --rebase
git pull origin develop
Push changes:
git push
git push -u origin feat/new-feature
git push --set-upstream origin feat/new-feature
git push origin feat/new-feature
git push --all origin
git push --tags
git push --force origin feat/new-feature
git push --force-with-lease origin feat/new-feature
Remote management:
git remote
git remote -v
git remote add origin https://github.com/user/repo.git
git remote set-url origin https://github.com/user/new-repo.git
git remote remove origin
git remote rename origin upstream
git remote show origin
Stashing
Save work temporarily:
git stash
git stash save "Work in progress on feature X"
git stash -u
git stash --include-untracked
git stash -a
git stash --all
git stash --keep-index
Apply stashed changes:
git stash list
git stash apply
git stash apply stash@{2}
git stash pop
git stash branch new-branch stash@{0}
Manage stashes:
git stash show
git stash show -p
git stash show stash@{1}
git stash drop stash@{0}
git stash clear
Tagging
Create tags:
git tag v1.0.0
git tag -a v1.0.0 -m "Release version 1.0.0"
git tag -a v1.0.0 <commit-hash> -m "Message"
List and show tags:
git tag
git tag -l
git tag --list
git tag -l "v1.*"
git show v1.0.0
Push and delete tags:
git push origin v1.0.0
git push --tags
git tag -d v1.0.0
git push origin --delete v1.0.0
git push origin :refs/tags/v1.0.0
Common Workflows Without Worktrees
Feature branch workflow:
git checkout main
git pull --ff-only
git checkout -b feat/new-feature
git add .
git commit -m "feat: implement X"
git fetch origin
git rebase origin/main
git push -u origin feat/new-feature
git checkout main
git pull --ff-only
git merge --no-ff feat/new-feature
git push origin main
git branch -d feat/new-feature
git push origin --delete feat/new-feature
Hotfix workflow:
git checkout main
git pull --ff-only
git checkout -b hotfix/critical-bug
git add .
git commit -m "fix: resolve critical bug"
git checkout main
git merge --no-ff hotfix/critical-bug
git tag -a v1.0.1 -m "Hotfix release"
git push origin main --tags
git checkout develop
git merge --no-ff hotfix/critical-bug
git push origin develop
git branch -d hotfix/critical-bug
Bisect for debugging:
git bisect start
git bisect bad
git bisect good v1.0.0
git bisect good
git bisect reset
Troubleshooting
Worktree Path Issues
Problem: "fatal: '' already exists"
git worktree list
git worktree prune
rm -rf <path>
git worktree add -b <branch> <path> <base>
Problem: "fatal: '' is not a working tree"
git worktree prune
rm -rf <path>
Branch Already Exists
Problem: "fatal: A branch named 'feat/task' already exists"
git branch -a | grep feat/task
git worktree add <path> feat/task
git branch -D feat/task
git worktree add -b feat/task <path> <base>
Lock Files
Problem: "fatal: 'worktrees/' is locked"
cat .git/worktrees/<name>/locked
rm .git/worktrees/<name>/locked
git worktree prune
Hook Not Blocking Wrong Branch
Problem: Able to commit on wrong branch despite hooks
HOOK_PATH="$(git rev-parse --git-path hooks/pre-commit)"
ls -la "$HOOK_PATH"
chmod +x "$HOOK_PATH"
GIT_DIR="$(git rev-parse --git-dir)"
cat "$GIT_DIR/agent-expected-branch"
cat "$GIT_DIR/agent-expected-worktree"
cd /path/to/AgentStackGuide
./scripts/setup-agent-guardrails.sh --target /path/to/your-repo --guardrails on --force
Common Workflows
Full Multi-Agent Setup
git fetch --all --prune
mkdir -p ../myapp-worktrees
git worktree add -b feat/codex-fix-auth ../myapp-worktrees/codex-fix-auth develop
git worktree add -b feat/claude-add-metrics ../myapp-worktrees/claude-add-metrics develop
git worktree add -b feat/copilot-docs ../myapp-worktrees/copilot-docs develop
cd ../myapp-worktrees/codex-fix-auth
GIT_DIR="$(git rev-parse --git-dir)"
printf '%s\n' "feat/codex-fix-auth" > "$GIT_DIR/agent-expected-branch"
printf '%s\n' "$(pwd -P)" > "$GIT_DIR/agent-expected-worktree"
git worktree list
Clean Merge and Cleanup
git log --oneline develop..feat/codex-fix-auth
git diff --stat develop..feat/codex-fix-auth
cd ~/src/myapp
git checkout develop
git pull --ff-only
git merge --no-ff feat/codex-fix-auth
git push origin develop
git worktree remove ../myapp-worktrees/codex-fix-auth
git branch -d feat/codex-fix-auth
git worktree prune
Discard Failed Branch
cd ~/src/myapp
git worktree remove ../myapp-worktrees/claude-add-metrics
git worktree remove --force ../myapp-worktrees/claude-add-metrics
git branch -D feat/claude-add-metrics
git push origin --delete feat/claude-add-metrics
git worktree prune
Git Configuration for Safety
Per-Worktree Settings
cd <worktree-path>
git config --local push.default current
git config --local pull.ff only
git config --local remote.pushDefault origin
Useful Aliases
git config --global alias.wt "worktree"
git config --global alias.wtl "worktree list"
git config --global alias.wta "worktree add"
git config --global alias.wtr "worktree remove"
git config --global alias.wtp "worktree prune"
git config --global alias.br "branch -vv"
git config --global alias.bra "branch -avv"
git config --global alias.pullff "pull --ff-only"
Advanced Operations
Move Worktree
git worktree remove <old-path>
git worktree add <new-path> <branch>
mv <old-path> <new-path>
git worktree list
git worktree prune
git worktree repair <new-path>
Repair Worktree
git worktree repair
git worktree repair <path>
Lock Worktree
git worktree lock <path>
git worktree lock <path> --reason "Agent currently working"
git worktree unlock <path>
Quick Reference
Worktree Operations
| Task | Command |
|---|
| Create worktree | git worktree add -b <branch> <path> <base> |
| List worktrees | git worktree list |
| Remove worktree | git worktree remove <path> |
| Clean metadata | git worktree prune |
| Set branch binding | printf '%s\n' "<branch>" > "$(git rev-parse --git-dir)/agent-expected-branch" |
| Set worktree binding | printf '%s\n' "$(pwd -P)" > "$(git rev-parse --git-dir)/agent-expected-worktree" |
Standard Git Operations
| Task | Command |
|---|
| Create branch | git checkout -b feat/name or git switch -c feat/name |
| Switch branch | git checkout main or git switch main |
| List branches | git branch -vv (local) or git branch -avv (all) |
| Delete branch | git branch -d name (safe) or git branch -D name (force) |
| Stage changes | git add . or git add file.js |
| Commit changes | git commit -m "message" |
| Amend commit | git commit --amend |
| View status | git status or git status -sb |
| View diff | git diff (unstaged) or git diff --staged (staged) |
| View history | git log --oneline --graph --all |
| Merge branch | git merge --no-ff feat/name |
| Rebase branch | git rebase main |
| Abort merge/rebase | git merge --abort or git rebase --abort |
| Undo last commit | git reset HEAD~1 (keep changes) |
| Discard changes | git restore file.js or git checkout -- file.js |
| Stash changes | git stash or git stash -u |
| Apply stash | git stash pop or git stash apply |
| Fetch updates | git fetch --all --prune |
| Pull changes | git pull --ff-only (safe) |
| Push changes | git push -u origin feat/name |
| View reflog | git reflog |
Safety Commands
| Task | Command |
|---|
| Verify branch | git branch --show-current |
| Verify path | git rev-parse --show-toplevel |
| Safe rebase | git rebase origin/develop |
| Safe push | git push origin <current-branch> |
| Safe pull | git pull --ff-only |
Error Prevention Checklist
Before starting agent work in a worktree: