Git Worktree Commands Cheatsheet with Practical Examples
Purpose
This post provides a quick reference guide to all git worktree commands. I’ll show you real-world usage examples for each command.
Git worktree has 7 main commands: add, list, lock, move, remove, prune, repair. Each handles specific aspects of managing multiple working directories.
1. git worktree add
The add command creates a new working directory linked to your repository.
Basic add with existing branch
git worktree add ../feat1 feat1This creates a new directory feat1 at ../feat1 and checks out the existing feat1 branch.
Add with new branch
git worktree add -b new-branch ../new-dir mainThe -b flag creates a new branch new-branch based on main in the new worktree.
Add in detached HEAD mode
git worktree add -d ../temp-workThe -d flag creates a worktree in detached HEAD state. Useful for quick experiments.
Add from a tag
git worktree add ../release-v1 v1.0.0You can checkout a specific tag or commit hash in a new worktree.
2. git worktree list
The list command shows all worktrees associated with your repository.
git worktree listExample output:
/main/repo 9ec7370 [main]/../feat1 d3bfa43 [feat1]/../temp-work a85d91a (detached)Each line shows:
- The worktree path
- The current commit hash
- The branch name (or detached state)
3. git worktree lock
The lock command prevents a worktree from being pruned or deleted.
git worktree lock ../feat1 --reason "Working on critical feature"The --reason flag documents why the worktree is locked. This helps team members understand the lock status.
4. git worktree unlock
The unlock command removes the lock from a worktree.
git worktree unlock ../feat1After unlocking, the worktree can be pruned or removed.
5. git worktree move
The move command relocates an existing worktree to a different path.
git worktree move ../feat1 ../new-locationThis moves the worktree from ../feat1 to ../new-location. Git updates all internal references automatically.
6. git worktree remove
The remove command deletes a worktree.
Basic remove
git worktree remove ../feat1This removes the worktree at ../feat1. The branch remains in the repository.
Force remove
git worktree remove -f ../feat1The -f flag forces removal even when the worktree has uncommitted changes or is locked.
7. git worktree prune
The prune command cleans up stale worktree references.
Basic prune
git worktree pruneThis removes worktree administrative data for worktrees that were manually deleted.
Dry run prune
git worktree prune -nThe -n or --dry-run flag shows what would be pruned without actually doing it.
8. git worktree repair
The repair command fixes worktree administrative files.
git worktree repairUse this after moving your main repository directory. It updates worktree paths and other administrative files.
Quick Reference Table
Here’s a summary of all git worktree commands:
| Command | Description | Common Flags |
|---|---|---|
add | Create new worktree | -b (new branch), -d (detached) |
list | Show all worktrees | None |
lock | Lock a worktree | --reason |
unlock | Unlock a worktree | None |
move | Move worktree to new path | None |
remove | Delete a worktree | -f (force) |
prune | Clean stale references | -n (dry run) |
repair | Fix worktree references | None |
Summary
In this post, I provided a quick reference guide for all git worktree commands with practical examples. The key commands are:
add- Create new working directories for different brancheslist- View all worktrees and their statuslock/unlock- Protect worktrees from accidental deletionmove- Relocate worktrees to different pathsremove- Delete worktrees (clean directory required)prune- Clean up after manually deleting worktree directoriesrepair- Fix references after moving the main repository
Git worktree helps you work on multiple branches simultaneously without the overhead of switching branches. It’s a powerful tool for developers who juggle multiple features or bug fixes.
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
- 👨💻 Git Worktree Documentation
- 👨💻 Git Worktree Tutorial
- 👨💻 Multiple Working Directories with Git Worktree
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments