DEV Community

Cover image for Tweak your Terminal for Git
Jason McCreary
Jason McCreary

Posted on • Originally published at

Tweak your Terminal 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 series. 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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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 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 file.

Next, we'll also include this file in the .bash_profile:

[[ -f "$HOME/" ]] && source "$HOME/"
Enter fullscreen mode Exit fullscreen mode

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" "\\\$ "'
Enter fullscreen mode Exit fullscreen mode

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 series. 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"
Enter fullscreen mode Exit fullscreen mode

Now if I open a new Terminal I'll see a prompt matches what we've seen in the Getting Git videos.

Want to learn more Git? Get full access to over 60 videos on Git commands and everyday scenarios when using Git in my video series Getting Git.

Top comments (9)

gonedark profile image
Jason McCreary • Edited

No fault. Homebrew is a great package manager and definitely makes it easy to install things on macOS. However, since macOS runs atop a Unix core, I've rarely found the need to use Homebrew. Often you can make and install directly from source. Admittedly a bit low-level, but I guess I'm used to it from my days as a C dev.

umardaraz profile image

I would add 97 things a programmer should know, to this list.
It is a amazing read containing 97 articles about different aspects of our job as a programmer

jkgatt profile image
Jean Gatt

Great article, but if you have homebrew installed there is much simpler way I think ->

gonedark profile image
Jason McCreary

Definitely do so if you're using homebrew. I'm not a fan of homebrew. I typically just install from source given the Unix core of macOS.

vigo profile image
Uğur "vigo" Özyılmazel

Hi! check out my tweaks :)

cmilr profile image
Cary Miller

Hey, great write-up! I've had a lot of fun tweaking my bash prompt for git recently. I'll have to give some of your suggestions a try.

plutov profile image
Alex Pliutau

Can be useful for terminal too!

pratikaambani profile image
Pratik Ambani • Edited

This is what I was looking for since long!!! Thanks Jason.
Can you suggest me how do we automate(schedule) commit and push at frequent intervals?

gonedark profile image
Jason McCreary

I don't make commits based on time. Check out When to make Git commits.