Last active
February 15, 2025 08:18
-
-
Save socketbox/665d1a91b86e93a58a0258c44ac56ef2 to your computer and use it in GitHub Desktop.
Add a git worktree within a workflow that uses tmux
This file contains 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
#!/bin/zsh | |
set -euo pipefail | |
#check to see that we're in the worktree root that contains the .bare directory | |
if [ ! -d .bare ]; then | |
echo "This script must be run from the root of the worktree" | |
exit 1 | |
fi | |
#check if an argument was passed to the script and if not print usage | |
if [ $# -eq 0 ]; then | |
echo "Usage: git_worktree_add.sh <new_worktree_name>" | |
exit 1 | |
fi | |
echo "Pulling on main" | |
cd ./main | |
# verify that we are in the main branch in two ways: | |
# 1. the output of git branch --show-current is main | |
# 2. the current working directory is the main worktree | |
if [[ $(git branch --show-current) != "main" || $(basename ${PWD}) != 'main' ]]; then | |
echo "You must be on and in main to run this script" | |
exit 1 | |
fi | |
git pull origin main | |
cd - | |
#create a new tmux window using the first argument to the script | |
tmux new-window -n $1 | |
#change to the new window | |
tmux select-window -t $1 | |
#sleep for 2 seconds to allow the new shell to be created | |
sleep 2 | |
#using the newly created shell of the new window, run the git worktree add command | |
tmux send-keys "git worktree add $1" C-m && tmux send-keys "cd $1" C-m | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment