Category: Git

Show Git branch & untracked/modified files in terminal

Screen clip of the final result

Many people know that you can add the Git branch name you are currently working in to the end of your command prompt but did you know that you can also have the command prompt notify you if you have any untracked of modified files?

Screen clip of the final result
Clip of the final result showing the Git branch name and one or more untracked files represented by the “U” after the branch name

I have created this post with linux in mind but I’m sure you can Google what to do if you have a different OS

First place the following in ~/.bashrc so that .bashrc can read my script.

# Include my very own git parse type function :-)
source ~/.GitParseStatus.sh

Then I update the PS1 variable (the command prompt text) to output the result of the script

export PS1=”\n${debian_chroot:+($debian_chroot)}${RED}\u@\H ${YELLOW}\A ${BLUE}\w \$(gitParseStatus)${NORMAL}\n$ ”

It’s the \$(gitParseStatus) bit that works the magic we want.

and then of course I have the script which I save as ~/.GitParseStatus.sh (don’t forget to make the script executable with

chmod +x ~/.GitParseStatus.sh

#!/bin/bash
#function get_out() {
# log $1
# return
#}
#
#function log() {
# echo '* '$1
#}
#
#
#if [ $1 = "--debug" ]; then
# DEBUG=true
# echo
# echo "==========="
# echo "Debug is on"
# echo "==========="
# echo
#fi
#
#=========================
# Set Variables
#=========================

# What do you want to show when everything is up to date
# Your options are…
TICK=$’\u2714′
OK=’OK’
CUSTOM_OK=”

# Now set the all ok message
ALL_OK=$TICK

# What do you want to display when there git needs updating in some way?
# I have used “M” for files have been modified
UPDATE_TEXT=’ M’

# What do you want to display when there are untracked files in the repo?
# I have used “U” but you can use whatever you want
UNTRACKED_TEXT=” U”

# How do you want to display the repo branch
# I have used square brackets
BRANCH_PRE_TEXT=”[”
BRANCH_POST_TEXT=”]”

# What colour do you want the git section of you command line to be when
# everything is ok?
# I have it as green
# Set some colours #
# Some examples are…
GIT_RESET=”17″
GIT_NORMAL=”33[0m”
GIT_RED_1=”33[31;1m”
GIT_YELLOW_1=”33[33;1m”
GIT_WHITE=”33[37;1m”
GIT_RED=”33[0;31m”
GIT_YELLOW=”33[01;33m”
GIT_GREEN=”33[0;32m”
GIT_BLUE=”33[01;034m”
BRANCH_COLOUR_ALL_OK=$GIT_GREEN
BRANCH_COLOUR_NOT_OK=$GIT_GREEN

TEXT_CODES_COLOUR_ALL_OK=$GIT_GREEN
TEXT_CODES_COLOUR_NOT_OK=$GIT_RED

function gitParseStatus {

# If we fail to get a branch name then simply return empty handed 🙁
#IN=$(git symbolic-ref HEAD 2> /dev/null) || echo “You are not in a Git repo”
IN=$(git symbolic-ref HEAD 2> /dev/null) || return
#echo $IN

BRANCH_NAME=`echo $IN | sed ‘s/refs\/heads\///’`
#log ‘Branch Name=”‘$BRANCH_NAME'”‘

# get a list of files to run through
FILES=$(git status -z 2> /dev/null)
#log ‘Git Status=”‘”$FILES”‘”‘

#echo
if [[ -z “$FILES” ]]; then

#log “All the files in your Git repo are up to date”
UPDATES=`echo -e ${TEXT_CODES_COLOUR_ALL_OK}`${ALL_OK}

BRANCH=`echo -e BRANCH_COLOUR_ALL_OK}`”$BRANCH_PRE_TEXT””$BRANCH_NAME””$BRANCH_POST_TEXT”

OUTPUT=”$BRANCH”

else

#log “You have discrepancies in your Git Repo”
BRANCH=`echo -e ${BRANCH_COLOUR_NOT_OK}`”$BRANCH_PRE_TEXT””$BRANCH_NAME””$BRANCH_POST_TEXT”

#log ‘Clearing the $UPDATES variable’
UPDATES=””

#echo ‘About to start testing on “‘${FILES}'”‘
#echo
#log ‘Testing for untracked files (ones beginning with ??)’
UNTRACKED=$(echo ${FILES} | grep $’\x000?{2}’)
#echo ‘UNTRACKED=”‘$UNTRACKED'”‘

if [ -n “$UNTRACKED” ] ; then

#log “Untracked files were found”
UPDATES+=”$UNTRACKED_TEXT”

else

UNTRACKED=$(echo ${FILES} | grep ^?{2})
#echo ‘UNTRACKED=”‘$UNTRACKED'”‘

if [ -n “$UNTRACKED” ] ; then

#log “Untracked files were found”
UPDATES+=”$UNTRACKED_TEXT”

fi

#log “No Untracked files were found”

fi
#echo
#log “Testing for modified files”

modified=$(echo ${files} | grep $’\x000[rmdc ]{2}’)
#echo ‘modified=”‘$modified'”‘
if [ -n “$modified” ] ; then]

#log “modified files were found”
updates+=”$update_text”

else

modified=$(echo ${files} | grep ‘^[rmdc ]{2}’)
#echo ‘modified=”‘$modified'”‘
if [ -n “$modified” ] ; then

#log “modified files were found”
updates+=”$update_text”

fi

#log “No Modified files were found”

fi

UPDATE_OUTPUT=`echo -e ${TEXT_CODES_COLOUR_NOT_OK}`$UPDATES
OUTPUT=$BRANCH$UPDATE_OUTPUT
fi
echo $OUTPUT
}

I hope it is of use to some of you and if you have any problems you know where I am.

John/Grandad