Git Worktrees
I keep all my worktrees in ~/worktrees/ and all my repos in ~/repos/.
🆕 Creating a worktree
Change directory into your ~/repos/[repo-name] and run the following to:
- Add a worktree
- Create a new branch
- Place the worktree in the selected path
- Optionally, pick a branch or commit to base the worktree on (I usually pick master)
git worktree add -b [new branch name] [path to worktree] [commit/branch]
💥 Listing worktrees
List out what worktrees exist:
git worktree list
~/repos/service_name 0000000 [master]
~/worktrees/service_name__branch_name 0000001 [feat/new_feature_one]
If you list your branches you can see which ones are worktrees (denoted by the +):
git branch
feat/new_feature_one
+ feat/new_feature_two
* master
Cleaning up
Once you’ve merged your branch, make sure you clean up after yourself:
git worktree remove [path to worktree]
😛 Gotchas
- Because a worktree is effectively a brand new
git cloneof the repo, you’ll need to initialize your environment again. - You’ll also need to copy any
.envor other ignored files into the worktree directory. - Generally, any files in the repo that git ignores will not carry over or be tracked.