Skip to content

Instantly share code, notes, and snippets.

@lkurzyniec
Created April 26, 2022 08:48
Show Gist options
  • Save lkurzyniec/93018499c53e06de3e97f90b0b43c258 to your computer and use it in GitHub Desktop.
Save lkurzyniec/93018499c53e06de3e97f90b0b43c258 to your computer and use it in GitHub Desktop.
git worktree

Creating a working tree from an existing branch

git worktree add ../app-example-2 other-feature

In this case, the git worktree command has three additional arguments:

  • add indicates want to create a new working tree
  • ../app-example-2 is the path to the folder where we want to create the working-tree. As we're running from the root-directory, this creates a folder called app-example-2 parallel to the clone folder.
  • other-feature is the name of the branch to check-out in the working tree

Creating a working tree from a new branch

git worktree add ../app-example-2 origin/main -b bug-fix

This example creates a new branch, bug-fix from the origin/main branch, and then checks it out at ../app-example-2.

Remove working tree

To remove it, run the following from your "main" working tree

git worktree remove ../app-example-2

If you have uncommitted changes in the linked working tree, git will block you from removing it. You can use --force to force the removal, if you're sure you want to lose the uncommitted changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment