There are a ton of uses for fzf and this is my absolute favorite. I work in a big monorepo with a team of nearly twenty engineers and it can be hard to find the branch I want since everyone is always pushing new branches. Fzf to the rescue!
The script gathers a list of all known branches, outputs them in a nice format, pipes that to fzf, and, once an option is selected, gets just the branch name from that selection, checks the branch out, and finally pushes the git checkout
command that was used to history.
That last command to push the command to history may not be compatible with all shells. I know it works in zsh, but cannot guarantee it works in others. I've noted in a comment that you may need to remove that line.
Here's the function (or you can see it in my zshrc):
gch() {
GCH_FORMAT="%(HEAD) %(refname:short) - %(objectname:short) - %(contents:subject) - %(authorname) (%(committerdate:relative))"
GCH_BRANCH="$(git branch --all --format="$GCH_FORMAT" | fzf | awk -F ' ' '{print $1}')"
if [ -n "$GCH_BRANCH" ]; then
git checkout "$GCH_BRANCH"
print -S "git checkout $GCH_BRANCH" # this may be zsh only
fi
}
The format can be customized with the GCH_FORMAT
variable; check out all the field options at the official git docs.
There's probably a cleaner way to do this; my bash script kung fu is weak, but I use this command every single day and it saves me a ton of time.