Skip to content

Instantly share code, notes, and snippets.

@DaniGuardiola
Last active January 22, 2025 22:17
Show Gist options
  • Select an option

  • Save DaniGuardiola/08d0db9738d29b28dc582355f6fb4781 to your computer and use it in GitHub Desktop.

Select an option

Save DaniGuardiola/08d0db9738d29b28dc582355f6fb4781 to your computer and use it in GitHub Desktop.
My fish shell commands for git worktrees with completions!
function wtl
git worktree list | tail -n +2 | while read -l line
set path (echo $line | awk '{print $1}' | sed 's!.*/!!')
set branch (git -C (echo $line | awk '{print $1}') symbolic-ref --short HEAD 2>/dev/null)
echo "$path -> $branch"
end
end
complete -c wtl -f
function wta
if test (count $argv) -ne 1
echo "usage: wta <branch>"
return 1
end
set branch $argv[1]
set dir (echo $branch | sed 's!\/!_!g')
set bare_repo (git rev-parse --git-common-dir)
# Add the worktree
git worktree add $bare_repo/$dir $branch
# Change directory to the new worktree
cd $bare_repo/$dir
end
function __fish_wta_complete_branches
git branch --format="%(refname:short)" 2>/dev/null
end
complete -c wta -f -a '(__fish_wta_complete_branches)'
function wtb
if test (count $argv) -lt 1 -o (count $argv) -gt 2
echo "usage: wtb <new-branch> [base-branch]"
return 1
end
set new_branch $argv[1]
# Use provided base branch if given
if test (count $argv) -eq 2
set base_branch $argv[2]
else
# Only determine HEAD branch if base-branch isn't provided
set base_branch (git remote show origin | grep 'HEAD branch' | awk '{print $NF}')
end
set dir (echo $new_branch | sed 's!\/!_!g')
set bare_repo (git rev-parse --git-common-dir)
# Ensure base branch is up-to-date if it's remote
if git show-ref --verify --quiet refs/remotes/origin/$base_branch
git fetch origin $base_branch
set branch_ref origin/$base_branch
else if git show-ref --verify --quiet refs/heads/$base_branch
set branch_ref $base_branch
else
echo "error: base branch '$base_branch' not found locally or on origin"
return 1
end
# Create the new branch without switching the current branch
git branch --no-track $new_branch $branch_ref
# Add the worktree for the new branch
git worktree add $bare_repo/$dir $new_branch
end
function __fish_wtb_complete_base_branch
git branch --format="%(refname:short)" 2>/dev/null
end
# TODO: only complete for second argument
complete -c wtb -f -a '(__fish_wtb_complete_base_branch)'
function wtd
if test (count $argv) -ne 1
echo "usage: wtd <worktree-dir>"
return 1
end
set worktree_dir $argv[1]
set bare_repo (git rev-parse --git-common-dir)
set full_path $bare_repo/$worktree_dir
if not test -d $full_path
echo "error: worktree directory '$worktree_dir' does not exist"
return 1
end
# Remove the worktree
git worktree remove $full_path
end
function __fish_wtd_complete_worktrees
git worktree list | tail -n +2 | awk '{print $1}' | sed 's!.*/!!'
end
complete -c wtd -f -a '(__fish_wtd_complete_worktrees)'
function wtcd
if test (count $argv) -ne 1
echo "usage: wtcd <worktree-dir>"
return 1
end
set worktree_dir $argv[1]
set bare_repo (git rev-parse --git-common-dir)
set full_path $bare_repo/$worktree_dir
if not test -d $full_path
echo "error: worktree directory '$worktree_dir' does not exist"
return 1
end
cd $full_path
end
complete -c wtcd -f -a '(__fish_wtd_complete_worktrees)'
function wts
git worktree list | tail -n +2 | while read -l line
set path (echo $line | awk '{print $1}')
set worktree (echo $path | sed 's!.*/!!')
echo "==> $worktree"
git -C $path status
echo
end
end
complete -c wts -f
function codewt
if test (count $argv) -ne 1
echo "usage: codewt <worktree-dir>"
return 1
end
set worktree_dir $argv[1]
set bare_repo (git rev-parse --git-common-dir)
set full_path $bare_repo/$worktree_dir
if not test -d $full_path
echo "error: worktree directory '$worktree_dir' does not exist"
return 1
end
code $full_path
end
complete -c codewt -f -a '(__fish_wtd_complete_worktrees)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment