Last active
September 17, 2019 18:27
-
-
Save danthedaniel/cd044decf0e0e439ad2545fa4997c99e to your computer and use it in GitHub Desktop.
.bash_profile/.bashrc snippet that will show git information (branch, commits ahead and behind) in your prompt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add the following code to ~/.bash_profile or ~/.bashrc | |
function git_branch() { | |
git rev-parse --abbrev-ref HEAD 2> /dev/null | |
} | |
function git_prompt() { | |
local branch=$(git_branch) | |
local deltas=$(git rev-list --left-right --count $branch...origin/$branch 2> /dev/null) | |
local stats=$(git diff --stat 2> /dev/null | tail -n 1) | |
if ! [ -z "$branch" ]; then | |
echo -n "[\[\033[0;36m\]$branch\[\033[0m\]" | |
if ! [ -z "$deltas" ]; then | |
local ahead=$(echo $deltas | awk '{ print $1 }') | |
local behind=$(echo $deltas | awk '{ print $2 }') | |
echo -n " | " | |
echo -n "↓\[\033[0;31m\]$behind\[\033[0m\] " | |
echo -n "↑\[\033[0;32m\]$ahead\[\033[0m\]" | |
fi | |
if ! [ -z "$stats" ]; then | |
local files=$(echo $stats | rg -oP '\d+ files? changed' | awk '{ print $1 }') | |
local insertions=$(echo $stats | rg -oP '\d+ insertions?' | awk '{ print $1 }') | |
local deletions=$(echo $stats | rg -oP '\d+ deletions?' | awk '{ print $1 }') | |
echo -n " | " | |
echo -n "\e[33m~${files:-0}\e[39m," | |
echo -n "\e[32m+${insertions:-0}\[\033[0m\]," | |
echo -n "\[\033[0;31m\]-${deletions:-0}\[\033[0m\]" | |
fi | |
echo "] " | |
fi | |
} | |
function prompt() { | |
PS1="\u@\h:\w\\$\[$(tput sgr0)\] $(git_prompt)" | |
} | |
PROMPT_COMMAND=prompt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment