Git Helpers and Other Utility Functions Using pick
Git and pick
git
can be a bit of a pain, especially when passing branches or commit hashes as args. Big projects have lots of branches and tons of commits. Finding and copying commit hashes to compare commits, for example, is awkward and slow. In my case, the awkwardness meant I wasn’t getting nearly as much mileage out of Git as I should have been.
Then I read about pick from thoughtbot. It’s a command line tool you can use to “fuzzy select anything”. Pipe it a list of options and an ncurses
interface will open, allowing you to fuzzy search the options and send the one you pick to stdout
.
Looking at the gif on pick’s README, I realized selecting branches and commit hashes would be a lot faster with fuzzy search, so I wrote some utility functions that invoke pick to do exactly that. Since then my usage of and satisfaction with Git have gone up drastically. Here are descriptions of the helper functions.
gbp [command]
: Pick a branch and pass it to command (gbp git checkout, gbp git merge, etc). If command is not passed, pick and copy branch name.gbpf [-s]
: Pick a branch, pick a modified file from this branch, and diff it. If-s
is passed, invokegit show
on the file.ghp [command]
: Pick a past commit and pass it to command. If command is not passed, pick and copy commit hash.ghpf [-s]
: Pick a commit, pick a modified file from this commit, and diff it. If-s
is passed, invokegit show
on the file.gbc [-b]
: Pick a branch and find out how far ahead or behind (number of commits) it is compared with the current branch. If -b is passed, pick both branches.gbca [-b]
: Like gbc, but shows names and hashes of commits.
The gif below gives an idea of just how effective pick
can be for these tasks, using ghpf
. Remember, this function is for first picking a commit, then picking one of the files that has changed since that commit, and diffing the file. At the end, ghpf
echoes the command it executed.
Think of how long it would take to run git log
and git diff
and build up that command in your text editor. Using pick
it takes a few seconds.
Other Helpers
Here’s an alias I use that I named psp
. Read it and see if you can guess what it does.
ps -ef | pick | awk '{print \$2}' | xargs echo -n | pbcopy
That’s right… Pick any process on your machine, parse and clean the PID
, and copy it to the clipboard.
Basically, pick
is your friend whenever you need to search a list of anything. For commands that generate big lists, pick
can be so effective that it changes how you think of and use these commands, which is what happened to me with git
.