Using tig to cherry-pick across branches
If you know my dev style, you know that I love
tig. For the past few years I’ve used it almost exclusively for one thing: staging/unstaging individual lines or chunks of code. For this task,
tig is without peer. It is so good at it that I neglected, for years, to explore the other features of
tig more fully.
Today I learned that it’s possible to
git cherry-pick commits across branches entirely in one session. To do this, first run
tig (I sometimes do this inside of
<Leader>g :Te<CR>:term tig<CR> - this opens
tig in a terminal nested inside of an
nvim session). This will open the main view, which shows the commits for the currently checked out branch, in all it’s DAG goodness. Pressing
r at this point will bring up the references view.
A git reference is a pointer to a git commit. Branches are references - human readable names that, while the name itself is static, point to different git commits over time as the branch evolves. The
tig references view shows all of the references you’ve interacted with.
Pick the branch you want to
git cherry-pick from and press
Enter. This will bring up a view similar to the main view, but for the branch you chose instead of the branch that is currently checked out. From here you can highlight the commit to pick, and press
C to bring up the
git cherry-pick prompt, which when confirmed will run the command as if you ran
git cherry-pick <commit SHA1> yourself.
This is a huge improvement over my previous workflow for cherry-picking. Before I used to have to check out the branch I wanted to cherry-pick from first, run a
git log to see the SHA1 of the commit to cherry-pick, then copy that SHA1 to my clipboard using my mouse, or with
tmux. Now I can do all of this without switching branches, and while staying entirely within my keyboard. Any time you can use a workflow that’s confined to the keyboard (and even better, home row), you should take the time to learn and incorporate it.
So that’s all there is to it. I suspect it’s always been possible to do this with
tig, and it’s a testament to the power of the tool that I only bothered to learn to do it now after getting years of mileage out of just the most basic use-case.
For anyone building tools, take note. Find a workflow that takes just a hair too long and build something that makes the workflow faster and simpler.