Helper scripts for git worktree
Convert an existing git repository for use with git-worktree
git-convert-for-worktree.sh <directory>git-convert-for-worktree.ps1 <directory>- Directory: The directory containing an existing git repository
Clone a git repository for use with git-worktree
git-clone-for-worktree.sh [--single-branch] <repository> [<directory>]git-clone-for-worktree.ps1 [-Repository] <string> [[-Directory] <string>] [-SingleBranch]- Repository: The repository to clone from.
- Directory: The directory to clone into. By default the basename of the repository without extension.
- Single Branch: Only fetch the main branch.
You can clone a repository similar to using git-clone with:
git-clone-for-worktree git@github.com:my-project/my-repo.gitThis creates a git repository in my-repo with a folder
containing one worktree tracking the primary branch (usually main).
Once you enter this worktree, you can use normal commands like git checkout
to checkout new and existing branches.
If a pressing change requires a hotfix, you can create a new hotfix worktree in the repository root with:
git worktree add -b urgent-bugfix hotfix mainThis creates a new urgent-bugfix branch in the new hotfix worktree
based off the main branch without disturbing your other worktrees.
For more information on worktrees, see the documentation for git-worktree.
Projects with many contributors can have large numbers of branches. Syncing every branch on these repositories takes a long time, so it is useful to limit which branches to sync.
Common practice is to prefix branch names with your username.
To setup a repository to have git pull sync only the main branch and branches with the prefix USERNAME/:
git-clone-for-worktree --single-branch git@github.com:my-project/my-repo.git
cd my-repo
git config --add remote.origin.fetch '+refs/heads/USERNAME/*:refs/remotes/origin/USERNAME/*'You can fetch and checkout other branches, for example OTHERUSER/their-branch, with:
git fetch origin OTHERUSER/their-branch:refs/remotes/origin/OTHERUSER/their-branch
git checkout origin/OTHERUSER/their-branch