Joe Violago

Trust me, I'm a dummkopf! [☉⏝⚆]

Bash Tweaks 1: git prompt

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
    • /usr/share/bash-completion/completions/git github
  3. If you see below, you’re good:
‘/etc/bash_completion.d/git-prompt’
1
2
3
4
5
6
7
8
9
# 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

‘.bashrc’
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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:

    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.

Comments