iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📚

Created gh-wt: A GitHub CLI extension for easier git worktree management

に公開

TL;DR

  • Created "gh-wt," a GitHub CLI extension for conveniently managing git worktrees
  • Interactive worktree selection and deletion using fzf
  • Easy installation via gh extension: gh extension install HikaruEgashira/gh-wt

Introduction

When working on multiple branches in parallel, you often find yourself forced to stash or commit just to switch branches—whether it's for Cline, PR reviews, etc. In such cases, git worktree is very useful, but many people likely customize it because the commands are long.

This is a follow-up to my previous gh extension "gh-q," so using them together will definitely make your environment more convenient.

https://zenn.dev/sqer/articles/4bfc05e7ec4eb4

Existing Solutions and Challenges

Basic usage of git worktree:

# Create a new worktree
git worktree add ../project-feature feature-branch

# List worktrees
git worktree list

# Remove a worktree
git worktree remove ../project-feature

It's quite long, isn't it?

Solution with gh-wt

A key feature is that installation is easy because it uses a gh extension.

gh extension install HikaruEgashira/gh-wt

Basic Usage

# List worktrees
gh wt list

# Create a new worktree
gh wt add feature-branch

# Remove a worktree (interactive selection)
gh wt remove

# Open VS Code in a worktree (interactive selection)
gh wt code

Practical Usage Example

For example, while working in ~/user/project:

# Create a worktree for a feature branch
$ gh wt add feature-login
Creating worktree for branch 'feature-login' at '/Users/user/project-feature-login'
Worktree created successfully!

# Open VS Code in the created worktree
$ gh wt code
# Select a worktree with fzf → VS Code launches

Technical Implementation

Automatic Branch Detection

The extension checks for the existence of local and remote branches and selects the appropriate method for creating a worktree:

if (cd "$selected_dir" && git show-ref --verify --quiet refs/heads/"$branch"); then
    # Local branch exists
    git worktree add "$worktree_path" "$branch"
elif (cd "$selected_dir" && git show-ref --verify --quiet refs/remotes/origin/"$branch"); then
    # Remote branch exists, create a tracking branch
    git worktree add "$worktree_path" -b "$branch" "origin/$branch"
else
    # Branch does not exist, create a new one
    git worktree add "$worktree_path" -b "$branch"
fi

Summary

Using gh-wt makes git worktree operations significantly more convenient. Especially in today's era of parallel work with agents, you will definitely feel the improvement in work efficiency.

Please give it a try! We look forward to your feedback and contributions.

GitHub: https://github.com/HikaruEgashira/gh-wt

Discussion