Terminal Tweaks for Git
Tweaking your terminal for Git will not only make it easier to use Git, but also make your fellow developers jealous.
Many people have asked about the terminal I use in the Getting Git video course. I'll include some of my personal tweaks in this post. But mainly, we'll add command completion and update our prompt for Git.
These tweaks will save time by autocompleting Git commands and references. In addition, a prompt which includes the branch name and status will provide immediate feedback about the current state of the Git repository.
Before getting started, let's take a step back to talk about platforms.
If you're on Windows, both command completion and a Git prompt are included with Git Bash. I recommend using this as it not only emulates a common shell, but will include the latest version of Git.
If you're on a Unix-based system, which includes macOS, your system should include a terminal application running a default shell. For example, macOS, which is what I'm on, runs the bash shell by default. While other shells are available, such as zsh or csh, bash is what I use.
Regardless of your shell, both updating your prompt and adding command completion has a similar setup. With that said, the examples in this post are specific to bash and macOS.
Git Command Completion
First, let's add command completion. Command completion will allow us to type part of a Git command or reference, such as a branch name, and press Tab to autocomplete the rest.
Within the contributions to Git open source project are scripts for command completion. Let's visit the repository for Git on GitHub. I'll open the one for bash, but there are more scripts available for other shells.
The script comments include steps for installing command completion. Let's follow these steps.
First, I'll view the raw version so it's easier to copy the contents of the file. Then I'll go back to Terminal.
While I could create a new file, paste the contents, and save the file. I can do this in one step on macOS using the pbpaste
command to output what I copied to a file named git-completion.bash
. For example:
pbpaste > git-completion.bash
The next step is to include this file when our shell starts.
To do so, the comments suggest adding the following line to the .bashrc
file.
However, macOS doesn't use this file by default. So instead, I'll add it to .bash_profile
. Don't worry if you don't have this file. You can create a one.
Let's add the following line to ensure the git-completion.bash
file exists and, if so, include the file.
[[ -f "$HOME/git_completion.bash" ]] && source "$HOME/git_completion.bash"
Upon saving we can start up a new Terminal for these changes to take effect.
Now you can test command completion. Type git chec
, Tab and see it autocompletes to git checkout
.
If something can't be autocompleted, pressing Tab again will display all the possible options.
For example, if I typed git ch
, Tab it autocompletes as much of the command as it can. Pressing Tab again displays all possible commands starting with che
.
In this case, checkout
, cherry
, and cherry-pick
.
Updating our prompt for Git
Next, let's update our prompt.
If we go back to scripts on GitHub, we'll also see git-prompt.sh
. This script is compatible with both bash and zsh.
Similar to before, we'll copy the raw contents of the file and paste them to a git-prompt.sh
file.
Next, we'll also include this file in the .bash_profile
:
[[ -f "$HOME/.git_prompt.sh" ]] && source "$HOME/.git_prompt.sh"
There's one more step. This file provides code for a Git prompt, it doesn't configure your prompt.
To do so, we'll add the following line as well:
PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\$ "'
Upon saving we can start up a new Terminal again for these changes to take effect.
You can navigate to a Git repository to see the new prompt.
Additional Tweaks
Now, this prompt doesn't match the prompt I use in the Getting Git video course. I wanted to cover the default setup before covering my personalizations.
Everything I have covered so far should be compatible with any Unix-based terminal running a popular shell, such as bash or zsh.
First, while macOS comes with a Terminal application, iTerm2 is a popular alternative. I find it has a crisper UI and to be more configurable.
In addition, there are dozens of color themes available for iTerm2. For example, I'm using a slightly modified version of the Tomorrow Night Eighties theme.
Finally, I customized my command prompt. You can copy it as well as other shell configuration files from my dotfiles
In this case, I customized the prompt to include the return status of the last command as well as streamlined some of the output.
I updated my .bash_profile
to include my .bash_prompt
by replacing the PROMPT_COMMAND
line we added above with:
[[ -f "$HOME/.bash_prompt" ]] && source "$HOME/.bash_prompt"
Now if I open a new Terminal I'll see a prompt matches what we've seen in the Getting Git videos.