Search code examples
bashgitgist

Bash script to show git username in terminal


Does anyone know how to show git username in the terminal?

I am using the following bash script for the branch, but, since I have several accounts I would like to show as well username or user email

BTW, I know I can use git config --global --list. The idea is to see the info in the terminal without having to check every time, as with the branch

parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "

Solution

  • You can use git config --get to get a single configuration value, like the username. Then, you can wrap it in a function:

    parse_git_user() {
        git config --get user.email
    }
    

    And then you can incorporate it into the prompt (PS1) in any format you wish. E.g.:

    export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] ($(parse_git_user))$ "
    # Here -----------------------------------------------------------^