A collection of shell aliases to simplify working with Git worktrees. These aliases provide convenient commands for creating, managing, and removing Git worktrees with automatic organization and branch handling.
- Automatic worktree organization: Worktrees are stored in a configurable base directory organized by repository
- Smart branch detection: Automatically detects if a branch exists locally or remotely
- Default branch fallback: Creates new branches from the repository's default branch when needed
- Configurable editor integration: Automatically opens new worktrees in your preferred editor
- Safe removal: Includes confirmation prompts and branch cleanup options
-
Clone the repository:
git clone https://github.com/peterargue/worktree.git ~/.git-worktree-aliases cd ~/.git-worktree-aliases
-
Run the install script:
./install.sh
The install script will:
- Detect your shell (bash, zsh, etc.)
- Add the aliases to your shell RC file (e.g.,
~/.bashrc,~/.zshrc) - Source the aliases automatically on shell startup
-
Reload your shell or restart your terminal:
source ~/.zshrc # or ~/.bashrc
Add a new worktree for a new or existing branch:
wt-add feature-branchThe wt-add command will:
- Check if the branch exists locally or remotely
- Create the worktree in
{WORKTREE_DEV_DIR}/{org}/{repo}-worktrees/{branch-name} - Handle branch creation from the default branch if needed
- Automatically open the new worktree in your configured editor
Creating worktrees from specific branches, tags, or commits:
For more control over the base of your new branch, the recommended approach is to first create the branch locally from your desired base, then create the worktree:
# Create a branch from a specific branch
git checkout -b sub-feature-a feature-branch
# Create a branch from a specific tag
git checkout -b release-branch v1.2.0
# Create a branch from a specific commit
git checkout -b hotfix-branch abc1234
# Then create the worktree from the existing local branch
wt-add sub-feature-aThis approach gives you full control over the branch's base while still leveraging the worktree organization features.
View all current worktrees:
wt-listShows a formatted list of all worktrees with their branch names and paths.
Resume work on an existing worktree:
wt-resume feature-branchThe wt-resume command will:
- Look up existing worktrees for the specified branch
- Open the worktree directory in your configured editor if found
- Display a helpful message if no worktree exists for the branch
Remove a worktree:
wt-rm feature-branchThe wt-rm command will:
- Remove the worktree directory
- Optionally delete the associated branch (with confirmation)
- Handle worktrees that may be in different locations
The base directory for worktrees is configurable by setting the WORKTREE_DEV_DIR variable at the top of the aliases file:
WORKTREE_DEV_DIR="$HOME/dev" # Default locationYou can change this to any directory you prefer:
WORKTREE_DEV_DIR="$HOME/projects" # Alternative locationThe editor used to open new worktrees is configurable by setting the EDITOR_CMD variable:
EDITOR_CMD="cursor" # Default editorYou can change this to any editor command you prefer:
EDITOR_CMD="code" # Visual Studio Code
EDITOR_CMD="vim" # Vim
EDITOR_CMD="emacs" # Emacs
EDITOR_CMD="subl" # Sublime TextWorktrees are organized by repository under the configured base directory:
$WORKTREE_DEV_DIR/
├── organization/
│ ├── repo-worktrees/
│ │ ├── feature-branch/
│ │ ├── bugfix-123/
│ │ └── hotfix-release/
│ └── another-repo-worktrees/
│ ├── main-feature/
│ └── experimental/
└── another-org/
└── some-repo-worktrees/
├── develop/
└── release-candidate/
- Git 2.5+ (for worktree support)
- Bash or Zsh shell
- Your preferred editor (optional, for automatic opening)
Aliases not working after installation:
- Ensure your shell RC file was updated:
grep -n "aliases" ~/.zshrc - Reload your shell:
source ~/.zshrc - Check if the aliases file exists:
ls -la ~/.git-worktree-aliases/aliases
Worktree creation fails:
- Ensure you're in a Git repository
- Check that the remote origin is properly configured
- Verify you have the necessary permissions
- Ensure the
WORKTREE_DEV_DIRdirectory exists and is writable
Editor not opening automatically:
- Ensure your configured editor is installed and in your PATH
- The editor command should be available in your terminal
- Check that the
EDITOR_CMDvariable is set correctly in thealiasesfile
To remove the aliases:
- Edit your shell RC file (
~/.zshrcor~/.bashrc) - Remove the line:
[ -f ~/.git-worktree-aliases/aliases ] && source ~/.git-worktree-aliases/aliases - Reload your shell
Feel free to submit issues and enhancement requests!