Skip to content

Instantly share code, notes, and snippets.

@HendrikPetertje
Last active August 29, 2023 07:37
Show Gist options
  • Save HendrikPetertje/6741a53dfaed66ca17ce9d7779317af3 to your computer and use it in GitHub Desktop.
Save HendrikPetertje/6741a53dfaed66ca17ce9d7779317af3 to your computer and use it in GitHub Desktop.
git worktrees
# - Clone the remote first (replace REMOTE, with git url)
# - Setup origin to be fetched/pulled after that.
# this way you'll be able to receive new branches made by other people/machines after the initial clone
git clone REMOTE --bare
# set fetch destinations and a default remote for pushing/pulling
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git config checkout.defaultRemote origin
# -----------------------------
# Functions for your dot-files:
# -----------------------------
# Add and initialize git worktree
function wtup() {
if [ $# -lt 1 ]; then
printf "No arguments provided ussage:\nwoup branch-name\n"
return 1
fi
if [ ! -f HEAD ]; then
printf "No HEAD file found, are you sure that you are in the bare-repo's root directory?\n"
return 1
fi
git worktree add "$1"
cd "$1"
# Setup a conditional wtinit function, this function will execute after the worktree is created.
# (example tmux named sessions with conditionally loaded zsh/bash functions)
if command -v wtinit &> /dev/null; then
# little function that runs yarn install & yarn husky:install
# or whatever you assign to it in different TMUX env files
wtinit
fi
}
# Remove worktree, either with arguments or a select menu
function wtdown() {
if [ -n "$1" ]; then
echo "Removing worktree $1"
git worktree remove "$1"
return;
fi
backup_ps3=$PS3
worktree_options=($(ls worktrees))
PS3="Select item please: "
select item in "${worktree_options[@]}" Cancel
do
# if cancel is selected
if [[ $REPLY == $(( ${#worktree_options[@]}+1 )) ]]; then
echo "Canceled"
PS3=$backup_ps3
return;
fi
# if input number is bigger than the number of items in the array
if [ -z "$item" ]; then;
echo "Oops, unknown choice."
else
PS3=$backup_ps3
echo "Removing worktree $item"
git worktree remove $item
return;
fi
done
}
# -----------------------------
# Functions for your session-specific dot-files:
# -----------------------------
# Are you using tmux then you can source named session specific dot-files!
# This is great if you want to source AWS keys per named session or expose certain aliases
# (like path aliases to your project or aliases to start development servers or test servers)
# In tmux you can start a named session like so:
# $> tmux new -s my-session-name
# You could even use this `.zshrc` snippet to quickly attach to a session:
# tmuxgo() {
# if [ $# -lt 1 ]; then
# echo "No arguments provided ussage:\ntmuxgo session-name"
# return 1
# fi
#
# if tmux has-session -t $1; then
# tmux attach -t $1
# else
# echo "Creating new session $1"
# tmux new -s $1
# fi
# }
# You can then use these 2 handy commands to call for session-specific dotfiles:
# TMUXENV=`tmux display-message -p '#S'`
# if [ -f ~/.dotfiles/tmux-envs/$TMUXENV.sh ]; then
# source ~/.dotfiles/tmux-envs/$TMUXENV.sh
# fi
# Enjoy!
# install all deps and install husky after going to a new worktree
function wtinit() {
# color codes
green='\033[0;32m'
reset='\033[0m'
echo -e "${green}-- Installing deps --${reset}"
yarn
echo -e "${green}-- Installing husky --${reset}"
yarn husky:install
echo -e "${green}-- Copying env file --${reset}"
cp .env.dev .env
echo -e "${green}-- DONE, have fun! --${reset}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment