Show a git branch using PROMPT COMMAND

suggest change

If you are inside a folder of a git repository it might be nice to show the current branch you are on. In ~/.bashrc or /etc/bashrc add the following (git is required for this to work):

function prompt_command {
    # Check if we are inside a git repository
    if git status > /dev/null 2>&1; then
        # Only get the name of the branch
        export GIT_STATUS=$(git status | grep 'On branch' | cut -b 10-)
        export GIT_STATUS=""
# This function gets called every time PS1 is shown

PS1="\$GIT_STATUS \u@\h:\w\$ "

If we are in a folder inside a git repository this will output:

branch user@machine:~$

And if we are inside a normal folder:


Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents