Git: autocomplete branch names in the command line

Learn how to add git autocomplete for commands and branch names in your bash or zsh terminal.
Published 2023-03-15 2 min read
Git: autocomplete branch names in the command line


Git is a popular version control system used by developers to track changes in code. One of its many features is the ability to create branches, allowing for parallel code development. However, with a large number of branches, it can be time-consuming to switch between them using the command line. This article will explore how to use autocomplete to switch between branches in Git quickly.


Step 1. Download autocomplete scripts

Git has autocomplete scripts for bash and zsh, so you need to download the correct ones. All scripts downloaded in this tutorial are from the official repository. If you don’t know what your shell type echo $0 in your terminal.


curl -o ~/.git-completion.bash


mkdir -p ~/.zsh
curl -o ~/.git-completion.bash
curl -o ~/.zsh/_gitcompletion


Step 2. Edit .bash_profile or .zshenv

Now we need to make use of downloaded files.


Edit ~/.bash_profile and add the followind line:

. ~/.git-completion.bash

It’ll source the Git completion script.


Edit ~/.zshenv (which is the bash_profile counterpart in zsh) and the following lines:

zstyle ':completion:*:*:git:*' script ~/.git-completion.bash
fpath=(~/.zsh $fpath)
autoload -Uz compinit && compinit


Step 3. Enjoy autocompletion in your terminal

Now new terminal sessions will have the ability to autocomplete and suggest git commands and branch names. Using autocomplete to switch between Git branches can save developers valuable time and streamline their workflow. By downloading the appropriate scripts and editing the shell profile, developers can easily utilize this feature in their command line. With autocomplete, switching between Git branches has never been easier.

#git #cli