DEV Community

Sotaro KARASAWA
Sotaro KARASAWA

Posted on

I Created a gw Command to Make Git Worktree More User-Friendly

I made it.

https://github.com/sotarok/gw

Motivation

  • Git Worktree is convenient, isn’t it?
  • But I can’t remember the commands for some reason
  • Essentially, what I want to do is start and end worktrees with “let’s do it” and “done”
  • Plus there are quite a few tedious associated tasks (like running npm install)

So I wanted something to support that feeling.

Similar Tools

After I started building this, I learned that someone was creating a much cooler integrated management tool called ccmanager.

https://github.com/kbwo/ccmanager

I thought “this would be fine,” but gw is more of a primitive tool that just makes git worktree work nicely.

Also, well, in times like these, I thought it might be okay to reinvent the wheel a lot, so I decided to create something that’s easy for me to use in my own way.

Features

The functionality is quite simple - it just handles adding and removing git worktrees based on issue numbers like this:

Creating a worktree

gw start 123
Enter fullscreen mode Exit fullscreen mode

This creates a worktree for issue #123.

When you do this, it creates a nice worktree directory named gw-{issueNumber} like:

sotarok
├── gw
├── gw-123
...
Enter fullscreen mode Exit fullscreen mode

If you want to specify a base branch, just append it at the end:

# Working on 123 branched from 122/impl branch
gw start 123 122/impl
Enter fullscreen mode Exit fullscreen mode

Finishing

gw end 123
Enter fullscreen mode Exit fullscreen mode

If you don’t include 123, it enters interactive mode where you can select.

Checking out

Sometimes when reviewing, you want to bring someone else’s branch locally without polluting your local repository to check various things, right?

In such cases:

gw checkout branch/name
Enter fullscreen mode Exit fullscreen mode

You can directly specify the branch name like this, and it will create a worktree for that purpose.

Extra Features

There’s no end to these kinds of additions, but I end up wanting to create them anyway, so I keep adding more and more.

Automatically running setup scripts

I wanted npm install or pnpm install to run automatically when doing gw start/checkout, so I made it do that.

It supports npm, yarn, pnpm, cargo, go, pip, and bundler.

Copy env

I want .env or .env.local to be automatically copied into the worktree.

So it copies them.

You can either add --copy-envs when doing start/checkout, or if not specified, it will ask you.

With these two behaviors working, you can just do gw start and immediately get to work.

Auto cd

Automatically moves to that directory when you do gw start/checkout.

Shell integration is required. The behavior can be controlled via config.

It’s good to add something like this to your .zshrc. It supports bash/zsh/fish. (Probably. I’ve only tested it with zsh)

eval "$(gw shell-integration --show-script --shell=zsh)"
Enter fullscreen mode Exit fullscreen mode

iTerm2 integration

When working on multiple things in parallel, you end up with lots of iTerm2 tabs and forget what you’re doing where, so it updates the iTerm2 tab name to repositoryName {issueNumber}. The behavior can be controlled via config.

Mac only.

Auto-delete head branch when ending worktree

I was bothered by local branches remaining when doing worktree remove. Now it auto-deletes them.

The behavior can be controlled via config.

How the gw command itself was made

  • Made with go
  • But I didn’t write a single line myself… hehe… (I made it, or rather had it made… I guess)
  • I’ve been adding features during work breaks and whenever I think of something

Actual Workflow (Use Case)

  • Find an issue I want to work on
  • Open a new iTerm2 tab
  • Navigate to the repository
  • Do gw start
  • Split the screen with tmux and work with claude etc.
  • After finishing work and getting to merge, do end

Something like that.

What I personally like is that gw is very easy to type.

Please try it out if you’d like.

Top comments (0)