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.
Bash:
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
ZSH:
mkdir -p ~/.zsh
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.zsh -o ~/.zsh/_gitcompletion
Step 2. Edit .bash_profile or .zshenv
Now we need to make use of downloaded files.
Bash:
Edit ~/.bash_profile
and add the followind line:
. ~/.git-completion.bash
It’ll source the Git completion script.
ZSH:
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.