Worktree-aware git checkout. Works identically to git checkout <branch>, but if the target branch is checked out in another worktree, it automatically:
- Stashes uncommitted changes in that worktree
- Removes the worktree
- Checks out the branch in the current working tree
- Restores the stashed changes
- Save the script to
~/bin/git-wcheckout.bash:
mkdir -p ~/bin
curl -o ~/bin/git-wcheckout.bash <SCRIPT_URL>
chmod +x ~/bin/git-wcheckout.bash- Add a git alias to
~/.gitconfig:
[alias]
wcheckout = !/Users/<USERNAME>/bin/git-wcheckout.bashNote:
git configescapes!, so edit~/.gitconfigmanually rather than usinggit config --global alias.wcheckout.
git wcheckout <branch> # switch to branch, even if it's in a worktree
git wcheckout feature-x --force # supports additional checkout arguments