Skip to content

Instantly share code, notes, and snippets.

@ElDeveloper
Last active October 13, 2015 13:57
Show Gist options
  • Save ElDeveloper/4206016 to your computer and use it in GitHub Desktop.
Save ElDeveloper/4206016 to your computer and use it in GitHub Desktop.
Current git branch in your bash prompt

Show the time, the current folder where you are working and the current branch if working on a git repository. Output looks like 19:41:40 Documents@master$ . If using an xterm-256 terminal emulator, hour should be green, cwd white and the branch red.

function egit (){                                                               
    function get_git_branch () {                                                
        command git rev-parse --abbrev-ref HEAD 2> /dev/null                    
    }                                                                           
    # git specific usage for branching                                          
    function branch_separator () {                                              
        if [[ -n $(get_git_branch) ]]                                           
        then                                                                    
        echo "@"                                                                
        fi                                                                      
    }                                                                           
    export TMP_GIT_PS1=${PS1} # back up the default settings                    
    export PS1='\[\e[1;32m\]\t\[\e[m\]:\W\[\e[01;38;5;195m\]$(branch_separator)\[\e[m\]\[\e[01;38;5;196m\]$(get_git_branch)\[\e[m\]$ '
}                                                                               
function dgit (){                                                               
    export PS1=${TMP_GIT_PS1}                                                   
}

# enable git flavored prompt
egit;
@gregcaporaso
Copy link

This is awesome - thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment