Skip to content

wnitisak/Git-Worktree-Management

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Git Worktree Management for Cursor

A comprehensive ZSH tool for managing Git worktrees, specifically designed to work seamlessly with Cursor editor and fix common worktree issues.

πŸ“‹ Table of Contents

✨ Features

  • 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

πŸš€ Installation

Prerequisites

  • ZSH shell
  • Git 2.5+ (with worktree support)
  • Cursor editor (optional, for gwcc command)

Install Steps

  1. Clone this repository:
git clone https://github.com/wnitisak/Git-Worktree-Management.git ~/.zsh/git-worktree
  1. Add to your ~/.zshrc:
# Add this line to ~/.zshrc
source ~/.zsh/git-worktree/functions.zsh
  1. Reload your shell:
source ~/.zshrc
  1. Verify installation:
gwc-help

🎯 Quick Start

Create Your First Worktree

# Method 1: Create only
gwc feature/my-feature

# Method 2: Create and open in Cursor (recommended!)
gwcc feature/my-feature

Navigate Between Worktrees

# List all worktrees
gwc-list

# Jump to a worktree
gwc-cd feature/my-feature

# Open in editor
gwc-open feature/my-feature

Remove Worktree When Done

gwc-rm-branch feature/my-feature

πŸ“š Commands Reference

Create & Open

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

List & Info

Command Description
gwc-list or gwc-ls Simple list of worktrees
gwc-info Detailed info (status, sync, changes)
gwc-branches List only branch names

Navigate & Open

Command Description Example
gwc-cd <branch> Navigate to worktree gwc-cd feature/ui
gwc-open <branch> [editor] Open in editor gwc-open main cursor

Remove

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

Maintenance

Command Description
gwc-health Check health of all worktrees
gwc-cleanup Prune stale references

Fix Cursor Issues

Command Description
gwc-detect-unlinked Scan for unlinked directories
gwc-link Interactive fix (select dir + branch)

Help

Command Description
gwc-help Show built-in documentation

πŸ”„ Workflows & Examples

Workflow 1: Feature Development

# 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

Workflow 2: Multiple Features

# 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-123

Workflow 3: Fix Cursor Auto-Created Directories

Problem: 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!

πŸ“Š Flowcharts

Main Workflow

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  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! βœ“              β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Cursor Fix Workflow

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  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! βœ“             β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Navigation Flow

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  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 βœ“    β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Decision Tree

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

πŸ”§ Advanced Configuration

Custom Worktree Location

By default, worktrees are created at:

~/.cursor/worktrees/<repo-name>/<branch-name>

To use custom location:

gwc my-branch --path ~/my-custom-location

Configure Search Paths for Main Repo

Edit 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"
)

🎨 Tab Completion

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 branches

πŸ†˜ Troubleshooting

Issue: "Branch doesn't exist"

Solution: Make sure the branch exists locally or remotely:

git branch -a  # Check all branches

Issue: "Can't find main repository"

Solution: The tool searches common locations. Either:

  1. Add your path to search_paths in functions.zsh
  2. Or specify manually: gwc <branch> --path /full/path

Issue: Tab completion not working

Solution: Reload your shell:

source ~/.zshrc

Issue: Worktree directory exists but not linked

Solution: Use the fix tool:

cd /path/to/main/repo
gwc-link

Issue: "Permission denied"

Solution: Check ownership of the worktrees directory:

ls -la ~/.cursor/worktrees
# If needed:
sudo chown -R $(whoami) ~/.cursor/worktrees

πŸ“– Documentation Files

🀝 Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

Development

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Testing

Before submitting:

# Check syntax
zsh -n functions.zsh

# Test in a new shell
zsh
source functions.zsh
gwc-help

πŸ“ License

This project is open source and available under the MIT License.

πŸ‘€ Author

wnitisak

πŸ™ Acknowledgments

πŸ“Š Project Stats

  • 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

About

ZSH tool for Git worktree management with Cursor editor integration

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages