A comprehensive ZSH tool for managing Git worktrees, specifically designed to work seamlessly with Cursor editor and fix common worktree issues.
- Features
- Installation
- Quick Start
- Commands Reference
- Workflows & Examples
- Flowcharts
- Troubleshooting
- Contributing
- Create & Manage Worktrees - Easy creation with automatic path management
- Cursor Integration - Open worktrees directly in Cursor editor
- Interactive Fix Tool - Fix Cursor auto-created directories that aren't linked to git
- Smart Navigation - Jump between worktrees by branch name
- Health Monitoring - Check status and sync of all worktrees
- Tab Completion - Intelligent completion for all commands
- Automatic Backups - Safe operations with automatic content preservation
- ZSH shell
- Git 2.5+ (with worktree support)
- Cursor editor (optional, for
gwcccommand)
- Clone this repository:
git clone https://github.com/wnitisak/Git-Worktree-Management.git ~/.zsh/git-worktree- Add to your
~/.zshrc:
# Add this line to ~/.zshrc
source ~/.zsh/git-worktree/functions.zsh- Reload your shell:
source ~/.zshrc- Verify installation:
gwc-help# Method 1: Create only
gwc feature/my-feature
# Method 2: Create and open in Cursor (recommended!)
gwcc feature/my-feature# List all worktrees
gwc-list
# Jump to a worktree
gwc-cd feature/my-feature
# Open in editor
gwc-open feature/my-featuregwc-rm-branch feature/my-feature| Command | Description | Example |
|---|---|---|
gwc <branch> |
Create worktree | gwc feature/api |
gwcc <branch> |
Create & open in Cursor | gwcc hotfix/bug |
gwc <branch> --path <path> |
Custom location | gwc develop --path ~/projects/dev |
gwc <branch> --open <editor> |
Open in specific editor | gwc main --open code |
| Command | Description |
|---|---|
gwc-list or gwc-ls |
Simple list of worktrees |
gwc-info |
Detailed info (status, sync, changes) |
gwc-branches |
List only branch names |
| Command | Description | Example |
|---|---|---|
gwc-cd <branch> |
Navigate to worktree | gwc-cd feature/ui |
gwc-open <branch> [editor] |
Open in editor | gwc-open main cursor |
| Command | Description | Example |
|---|---|---|
gwc-rm-branch <branch> |
Remove by branch name | gwc-rm-branch old-feature |
gwc-rm <path> |
Remove by path | gwc-rm /path/to/worktree |
| Command | Description |
|---|---|
gwc-health |
Check health of all worktrees |
gwc-cleanup |
Prune stale references |
| Command | Description |
|---|---|
gwc-detect-unlinked |
Scan for unlinked directories |
gwc-link |
Interactive fix (select dir + branch) |
| Command | Description |
|---|---|
gwc-help |
Show built-in documentation |
# 1. Start new feature
gwcc feature/user-authentication
# 2. Work on it... (Cursor opens automatically)
# 3. Check status anytime
gwc-info
# 4. Done? Remove it
gwc-rm-branch feature/user-authentication# Create multiple worktrees
gwcc feature/api
gwcc feature/ui
gwcc hotfix/bug-123
# See all at once
gwc-info
# Jump between them
gwc-cd feature/api
gwc-cd feature/ui
gwc-cd hotfix/bug-123Problem: Cursor created a directory but didn't link it to git branch.
Solution:
# 1. Go to your main repo
cd /path/to/your/repo
# 2. Scan for issues
gwc-detect-unlinked
# 3. Fix interactively
gwc-link
# β Select directory from list
# β See all branches
# β Pick the right one
# β Done!βββββββββββββββββββββββββββββββββββββββββββββββ
β Start: Need to work on different branch β
βββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββ
β Create Worktree β
β gwcc feature/branch β
βββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββ
β Cursor Opens β
β Auto-navigates β
βββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββ
β Work on Code β
β Commit changes β
βββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββ
β Feature Complete? β
βββββββββββββββββββββββββ
β
Yes βββββΌββββ No (continue working)
β
βΌ
βββββββββββββββββββββββββ
β Remove Worktree β
β gwc-rm-branch β
βββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββ
β Done! β β
βββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββ
β Problem: Cursor created unlinked directory β
βββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββ
β cd main-repo β
β gwc-detect-unlinked β
βββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββ
β Shows unlinked directories: β
β [1] temp-work β
β [2] quick-fix β
β [3] testing β
βββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββ
β Run: gwc-link β
βββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββ
β Select directory: 1 β
βββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββ
β Shows all branches: β
β β’ main β
β β’ develop β
β β’ feature/api β
β β’ feature/ui β
β β’ hotfix/bug β
βββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββ
β Enter: feature/api β
βββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββ
β β Backup content β
β β Create git worktree β
β β Link to branch β
β β Restore content β
βββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββ
β Fixed! β β
βββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββ
β Need to switch between worktrees? β
βββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββ΄ββββββββββββ
β β
βΌ βΌ
ββββββββββββββββ ββββββββββββββββ
β Know the β β Not sure β
β branch? β β which one? β
ββββββββββββββββ ββββββββββββββββ
β β
βΌ βΌ
ββββββββββββββββ ββββββββββββββββ
β gwc-cd β β gwc-list β
β feature/api β β (see all) β
ββββββββββββββββ ββββββββββββββββ
β β
β βΌ
β ββββββββββββββββ
β β gwc-cd β
β β <branch> β
β ββββββββββββββββ
β β
βββββββββββββ¬ββββββββββββ
β
βΌ
βββββββββββββββββββββββββ
β Now in worktree β β
βββββββββββββββββββββββββ
Which command should I use?
Want to start working on a branch?
β
ββ Branch exists? β Yes β gwcc <branch>
β (Creates + opens in Cursor)
β
ββ Branch not created yet β Create branch first
then: gwcc <branch>
Need to switch between branches?
β
ββ Use: gwc-cd <branch>
(Tab completion available!)
Have unlinked Cursor directories?
β
ββ Check first β gwc-detect-unlinked
β
ββ Fix them β gwc-link
(Interactive selection)
Want to see all worktrees?
β
ββ Simple list β gwc-list
β
ββ Detailed info β gwc-info
(shows status, changes, sync)
Done with a worktree?
β
ββ Remove it β gwc-rm-branch <branch>
(Safe removal)
Something broken?
β
ββ Check health β gwc-health
β
ββ Clean up β gwc-cleanup
By default, worktrees are created at:
~/.cursor/worktrees/<repo-name>/<branch-name>
To use custom location:
gwc my-branch --path ~/my-custom-locationEdit functions.zsh to add your custom paths:
# Around line 858-865
local -a search_paths=(
"$HOME/projects/$repo_name"
"$HOME/git/$repo_name"
"$HOME/your-custom-path/$repo_name" # Add your path here
"$HOME/$repo_name"
"$HOME/Documents/$repo_name"
"$HOME/code/$repo_name"
"$HOME/workspace/$repo_name"
)All commands support intelligent tab completion:
gwc <TAB> # Shows all available branches
gwcc hot<TAB> # Filters to branches starting with "hot"
gwc-cd <TAB> # Shows only existing worktree branches
gwc-open <TAB> # Shows existing worktree branches
gwc-rm-branch <TAB> # Shows existing worktree branchesSolution: Make sure the branch exists locally or remotely:
git branch -a # Check all branchesSolution: The tool searches common locations. Either:
- Add your path to
search_pathsinfunctions.zsh - Or specify manually:
gwc <branch> --path /full/path
Solution: Reload your shell:
source ~/.zshrcSolution: Use the fix tool:
cd /path/to/main/repo
gwc-linkSolution: Check ownership of the worktrees directory:
ls -la ~/.cursor/worktrees
# If needed:
sudo chown -R $(whoami) ~/.cursor/worktrees- functions.zsh - Main implementation
- README.md - Complete command reference
- QUICKSTART.md - Quick start guide
Contributions are welcome! Please feel free to submit issues or pull requests.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Before submitting:
# Check syntax
zsh -n functions.zsh
# Test in a new shell
zsh
source functions.zsh
gwc-helpThis project is open source and available under the MIT License.
wnitisak
- GitHub: @wnitisak
- Repository: Git-Worktree-Management
- Git worktree feature: Git Documentation
- Cursor editor: Cursor
- ZSH completion system
- Language: Shell (ZSH)
- Lines of Code: ~1,000
- Commands: 15+
- Documentation: Complete with flowcharts
- Status: Production Ready β
β Star this repo if you find it useful!
π Found a bug? Open an issue
π‘ Have an idea? Start a discussion