Last active
September 3, 2025 12:03
-
-
Save HendrikPetertje/6741a53dfaed66ca17ce9d7779317af3 to your computer and use it in GitHub Desktop.
git worktrees
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
| # Worktrees are an awesome way to separate the various branches of a git repo in to different directories. | |
| # each with their own git ignored dependencies, etc. | |
| # More info on this: https://www.youtube.com/watch?v=2uEqYw-N8uE&pp=ygUNZ2l0IHdvcmt0cmVlcw%3D%3D | |
| # - 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 | |
| # - Create a new worktree from the existing main or master branch | |
| git clone REMOTE --bare | |
| cd your-project.git | |
| # 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 | |
| git worktree add main # or master depending on what your main branch is |
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
| # ----------------------------- | |
| # Functions for your dot-files: | |
| # ----------------------------- | |
| # Add and initialize a new branch and create a git worktree directory | |
| 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 | |
| # you can see its implementation in the extra-named-session-hooks thingie below this file | |
| 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 | |
| } |
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
| # ----------------------------- | |
| # 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