Skip to content

Instantly share code, notes, and snippets.

@kigster
Created July 6, 2026 05:45
Show Gist options
  • Select an option

  • Save kigster/028224b2041aaafac87d0ef55ba2dbfb to your computer and use it in GitHub Desktop.

Select an option

Save kigster/028224b2041aaafac87d0ef55ba2dbfb to your computer and use it in GitHub Desktop.
gwt() — Bash Function to select a worktree via fzf using the worktree folder and a branch
# vim: ft=bash
gwt() {
local sel dir esc=$'\033'
sel=$(git worktree list --porcelain | awk -v home="$HOME" -v esc="$esc" '
/^worktree / { wt = $2; sub("^" home, "~", wt) }
/^branch / { sub(/^refs\/heads\//, "", $2)
printf "%s[1;33m%s%s[0m (%s[1;32m%s%s[0m)\n", esc, wt, esc, esc, $2, esc }
/^detached$/ { printf "%s[1;33m%s%s[0m (detached)\n", esc, wt, esc }
' | fzf --ansi \
--prompt='worktree > ' --height=40% --reverse --no-multi \
--color='fg+:bright-white:bold,bg+:238,hl:yellow,hl+:bright-yellow:bold,gutter:-1,pointer:red:bold') || return
dir=${sel%%" ("*}
dir=${dir/#\~/$HOME}
git config --global --add safe.directory "$dir"
cd "$dir" || return
}
@kigster

kigster commented Jul 6, 2026

Copy link
Copy Markdown
Author

This is what it looks like in practice:

CleanShot 2026-07-05 at 22 45 33@2x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment