Bash Tweaks 1: git prompt
Tags: bash, git
Published: 2023-02-13 21:51:39

Originally posted on [joev.me](http://blog.joev.me/blog/2013/12/23/Bash-Tweaks-1-git-prompt/) @ 2013-12-23

I can best describe my work environment in three words – git, bash & tabs. While it is important to have a basic level of competancy in bone-stock environments, I am also a firm believer in optimizing my workspace for the sake of comfort and productivity.

I mainly do my git fiddlin’ in bash console. Occasionally, I’ll use GitHub for investigating blames & history crawling. But my exposure & increased competence in git cli has taught me to appreciate a spruced up git config. Tour of my git toolbelt after the break.

Though bash is the core example of minimalist HUD (and simple is güd), I definately feel that a plain command prompt is too bare to productively manipulate & traverse branches.

> _I don’t know where I am (locally) compared to origin._

This problem really presents itself if/when my local `HEAD` has diverged from a merge conflict, working on out-of-date branches, or if a collaborator likes to push right before I do. (I’m looking at you, ~~Montero~~ (ﾉಥ益ಥ）ﾉ﻿ ┻━┻)

Solution(s)?
- Option 1: **$** `git status` my fingers numb (yeowch!)
- Option 2: Configure a hotkey in `.inputrc` (more on that later…)
- Option 3: Setup `.bashrc` to always show a mini `git status`

Here are some limitations to each though:

1. Excessive typing; finger fatigue…
2. Commands are now mapped to a hardware-specific input, which may not work when remoting in from another machine.
3. Some basic knowledge in shell scripting is required.

Today, Option 3.

---

## Dependencies

1. Install git
2. Verify files or get them.
    - `/usr/lib/git-core/git-sh-prompt` [github](https://github.com/git/git/blob/master/contrib/completion/git-completion.bash)
    - `/usr/share/bash-completion/completions/git` [github](https://github.com/git/git/blob/master/contrib/completion/git-completion.bash)
3. If you see below, you’re good:

`/etc/bash_completion.d/git-prompt`

```bash
# In git versions < 1.7.12, this shell library was part of the
# git completion script.
#
# Some users rely on the __git_ps1 function becoming available
# when bash-completion is loaded.  Continue to load this library
# at bash-completion startup for now, to ease the transition to a
# world order where the prompt function is requested separately.
#
. /usr/lib/git-core/git-sh-prompt
```

---

## Setup bashrc

Copy into `~/.bashrc`

```bash
# Setup git autocompletion
if [ -f /usr/share/bash-completion/completions/git ]; then
  . /usr/share/bash-completion/completions/git
fi

# Setup git PS1 integration
if [ -f /usr/lib/git-core/git-sh-prompt ]; then
    . /usr/lib/git-core/git-sh-prompt
fi

GIT_PS1_SHOWCOLORHINTS      = "true"
GIT_PS1_SHOWUPSTREAM        = "verbose count git"
GIT_PS1_SHOWSTASHSTATE      = "1"
GIT_PS1_SHOWDIRTYSTATE      = "1"
GIT_PS1_SHOWUNTRACKEDFILES  = "1"

# Uncomment one of these
# no-color
# PS1='\u@\h:\w\n\d [\T]\n$(__git_ps1)\$ '
# color
# PS1='\[\033[32m\]\u@\h\[\033[00m\]:\[\033[34m\]\w\n\[\033[00m\]\d [\T]\n\[\033[31m\]$(__git_ps1)\[\033[00m\]\$ '
```

Apply changes to current terminal session:

**$** `. ~/.bashrc`

Your bash prompt should now look like either of these:

```bash
  user@host:/home/user/git-path
    Mon Dec 23 [12:21:43]
    (master u=)$ _

    user@host:/home/user/git-path
    Mon Dec 23 [12:21:43]
    (master u=)$ _
```

And fin. Gone are the days of:

> _what branch am I on in this window?_
> _(slam out more `git status`)_

But kidding aside, this addition to my prompt has significantly cut down my frequency in calling `git status`. It is totally worth the minimal effort to implement.

---

## Explained

That `PS1` looks pretty cryptic, eh? Most of the script is mainly shell script. You can read more about tweaking bash prompt here. The secret sauce (and purpose of this post) is `$(__git_ps1)`, which comes from `/usr/lib/git-core/git-sh-prompt`. If you’re itchy to customize, I invite you to read into the file to learn all about `git_ps1`’s different settings.

Written with [StackEdit](https://stackedit.io/).