Skip to main content

Initialize your environment

I keep all my worktrees in ~/worktrees/ and all my repos in ~/repos/

Do whatever you want, but this is da wae.

🆕 Creating a worktree

Change directory into your ~/repos/[repo-name]

Run the following to do all of the following

  • Add a worktree
  • Create a new branch
  • Place the work tree in the selected path
  • Optionally, pick a branch or commit to base the work tree on, I usually pick master.
git worktree add -b [new branch name] [path to worktree] [commit/branch]

💥 List Worktrees

List our what work trees 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

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 clone of the repo, you'll need to initialize your environment again.
  • You'll also need to copy any .env or other ignored files into the worktree directory.
  • Generally, any files in the repo that git ignores will not carry over or be tracked (duh)