Skip to content

Instantly share code, notes, and snippets.

@alexng353
Created February 7, 2026 20:15
Show Gist options
  • Select an option

  • Save alexng353/8c14ab36c3c341dd64166ce7be48c865 to your computer and use it in GitHub Desktop.

Select an option

Save alexng353/8c14ab36c3c341dd64166ce7be48c865 to your computer and use it in GitHub Desktop.
Git worktree helper. Because it cd's for you, you have to `source work.sh` in your `~/.bashrc` or `~/.zshrc`.
work () {
cd ~/my-base-repository.git
git fetch
if [ -n "$1" ]
then
if [ -d "$1" ]
then
cd "$1"
elif git show-ref --verify --quiet "refs/heads/$1"
then
echo "Local branch '$1' already exists - creating worktree"
git worktree add "$1" "$1"
cd "$1"
bun i
elif git show-ref --verify --quiet "refs/remotes/origin/$1"
then
echo "Branch '$1' exists on remote - creating worktree tracking origin/$1"
git worktree add --track -b "$1" "$1" "origin/$1"
cd "$1"
bun i
else
echo "Branch '$1' not found anywhere - creating new branch"
git worktree add -b "$1" "$1"
cd "$1"
bun i
fi
else
cd main
fi
}
@alexng353
Copy link
Author

alexng353 commented Feb 7, 2026

The echo's are for debugging, so you can remove them if you want (they don't affect anything, so why would you)

usage:

work alex/pf-118-feat-some-issue

end result:

$ pwd
~/my-base-repository.git/alex/pf-118-feat-some-issue

Only works with git worktrees. That's the documentation link, or you can watch The Primeagen's video

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