Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Add a Git Branch to Your Prompt with a Few Lines of Shell Scripting

blog/cards/add-a-git-branch-to-your-prompt-with-a-few-lines-of-shell-scripting.jpg

This will work if you're using bash or zsh and it doesn't require installing any third party tools or plugins.

Quick Jump: Demo Video

There’s a lot of fancy solutions for this, some of which add many different characters to your prompt to determine not only which branch you’re on but also if you have changes to commit, if you’ve pulled the latest commits from your upstream, various git config options and more. This translates to hundreds of lines of code.

Personally I haven’t felt the need to have all of that. Here’s the main things I care about:

  • It evaluates quickly because it happens every time you change directories
  • It’ll show nothing if you’re not in a git repo
  • It’ll show the current branch that’s checked out
  • It’ll truncate a long branch name so it doesn’t take up a ton of screen space

That’s enough for me because my code editor will handle showing me what’s not commit and other details. Technically it shows the branch too but it’s useful to see the branch in your shell before you open your editor, especially if that’s where you might be running git commands to stage, commit and push your work.

I briefly talked about this solution a while back when I switched to zsh but never made a dedicated video on it until now. The video below goes over the code and breaks it down.

Demo Video

Code

# Drop this into your .zshrc or .bashrc file:
git_prompt() {
    local branch="$(git symbolic-ref HEAD 2> /dev/null | cut -d'/' -f3)"
    local branch_truncated="${branch:0:30}"
    if (( ${#branch} > ${#branch_truncated} )); then
        branch="${branch_truncated}..."
    fi

    [ -n "${branch}" ] && echo " (${branch})"
}

# This is specific to zsh but you could call $(git_prompt) in your .bashrc PS1 too.
setopt PROMPT_SUBST
PROMPT='%B%{$fg[green]%}%n@%{$fg[green]%}%M %{$fg[blue]%}%~%{$fg[yellow]%}$(git_prompt)%{$reset_color%} %(?.$.%{$fg[red]%}$)%b '

Timestamps

  • 0:30 – Taking a quick look at the end result in my zshrc file
  • 1:02 – Running the demo script and making a new git repo
  • 1:44 – Echoing the branch out if we’re in a git repo
  • 2:14 – Getting the branch pointing to your git HEAD
  • 2:59 – Parsing out just the git branch
  • 3:35 – Truncating the length of the branch if needed
  • 5:37 – Using bash to compare the length of 2 strings
  • 7:14 – Why I don’t include more bells and whistles in my git prompt

Which git details do you like to see in your prompt? Let me know below.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per month (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.



Comments