Skip to content

Instantly share code, notes, and snippets.

@leemeichin
Created March 6, 2020 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leemeichin/e80277d7983d085d6e1e202c0aa0bbcb to your computer and use it in GitHub Desktop.
Save leemeichin/e80277d7983d085d6e1e202c0aa0bbcb to your computer and use it in GitHub Desktop.
worktree.sh

Usage:

worktree init master sets your repo up for worktrees. e.g. if you have a repo called noel-edmonds the result would become this:

/noel-edmonds # container directory for your worktrees
  |- /master # main worktree branch (do not check out new branches from here!)
    |- .git/

worktree add BLOBBY-123-deal-or-no-deal master: creates a new worktree from the master branch when run inside your base worktree. The result would look like this:

/noel-edmonds
  |- /master # main worktree branch
  |- /BLOBBY-123-deal-or-no-deal # your new branch

Open up the directory and code like you normally do.

worktree() {
set -e
case $1 in
init)
local projectdir=$(git rev-parse --show-toplevel)
local projectname=$(basename $projectdir)
local basebranch=${2:-master}
git checkout $basebranch
# go to directory above repo
cd $projectdir/..
# move to temporary location
mv $projectname ${projectname}_
mkdir $projectname
mv ${projectname}_ $projectname/$basebranch
cd $projectname/$basebranch
echo 'Run `worktree add {branch-name} {base-branch}` to build a new worktree'
;;
add)
git worktree add -b "$2" ../$2 ${3:-master}
cd ../$2
;;
esac
set +e
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment