Skip to content

Fuzzy Search for Your Git Repository


Created: – Last Updated:

In this article, you’ll learn how to use fzf-git (opens in a new tab) to fuzzy search your local git repository.

Here’s what a branch search looks like:

A screenshot of a terminal window. The command "gcos" was run and underneath it two entries in a list are shown: main and revamp-photos. These represent branch names. Divided with a horizontal ruler below these two items a vertical list of commits is shown. These commits correspond with the selected/highlighted branch.

You can also use it to search through your files, tags, and more inside your CLI. The screenshot above shows a custom zsh function gcos() which uses fzf-git under the hood. You can also use fzf-git directly though.

Installation

  1. Install fzf (opens in a new tab).

  2. Install bat (opens in a new tab) for advanced syntax highlighting.

  3. Locate your .bashrc or .zshrc file (typically in your $HOME directory). Download the fzf-git.sh file from fzf-git (opens in a new tab) to this directory or a subdirectory.

  4. Source fzf-git.sh in your .bashrc or .zshrc file:

    .zshrc
    source "$HOME/fzf-git.sh"
  5. Try out the keybindings defined in fzf-git usage section (opens in a new tab). For example, CTRL-G CTRL-B for branches.

Defining shortcuts

To get the gcos() function shown in the screenshot, define a shortcut like so:

.zshrc
# Fuzzy-search branches
gcos() {
_fzf_git_branches --no-multi | xargs git checkout
}

You can check the fzf-git.sh script for a list of all available functions.