Skip to content

Instantly share code, notes, and snippets.

@mmichaelis
Last active April 30, 2018 17:25
Show Gist options
  • Save mmichaelis/391f7b1f75e2463682bf475ec9275b0a to your computer and use it in GitHub Desktop.
Save mmichaelis/391f7b1f75e2463682bf475ec9275b0a to your computer and use it in GitHub Desktop.
Creates a git working tree following some conventions.
@('feature-1', 'bug-2') | % {
git worktree add -b $_ "$(git rev-parse --show-toplevel)\..\$_" "origin/$_"
}

Description

Expecting that you are inside your repository you want to add a worktree for, you can just call this script. git rev-parse --show-toplevel will ensure that the new worktree is created as sibling of your repository.

It will create worktrees for all the given branches as siblings to your repository root folder. Instead of specifying an array of branches, you may just specify a string at the beginning.

A proposed local folder structure is as follows:

github/user/repository/branch

where you typically start with

github/user/repository/master

and then add a worktree like

github/user/repository/bug-2

Requirements

  • Your current directory is within the repository you want to create a worktree for.

  • The branch you want to create the worktree for is already available at remote.

    So this one is especially meant to create worktrees for development, maintenance and release branches.

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