Created
February 7, 2026 20:15
-
-
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`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The echo's are for debugging, so you can remove them if you want (they don't affect anything, so why would you)
usage:
end result:
Only works with git worktrees. That's the documentation link, or you can watch The Primeagen's video