Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know you can do something like this for the PS1:

Corvid @ ~/github/gitdirectory (master) $

but, this might be a bit odd: I want to only show the basename of the git directory, no matter where I am in that directory, prefixed with nothing. If I am not in a git directory, I do not want to display anything

For example, if I am home I want it to display:

Corvid $

If I have entered a git directory, I want it to display:

Corvid @ git_project (master)

and if I am moving around within that directory, I do not want it to add anything to the path. So if I am at ~/git/git_project/users/ or something, I only want it to display Corvid @ git_project (master)

How can this be achieved with PS1?

share|improve this question
up vote 1 down vote accepted

My PS1-fu is not that good, but I think this might work:

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

parse_git_tag () {
    git describe --tags 2> /dev/null

function git_prompt () {
    local OUT=
    local GIT_ROOT=$(git rev-parse --show-toplevel 2>/dev/null)

    if [[ -n $GIT_ROOT ]]
        OUT=" @ $(basename '$GIT_ROOT')"
        local GIT_BRANCH="$(parse_git_branch)"
        if [[ "$GIT_BRANCH" == " ((no branch))" ]]
        OUT="$OUT $GIT_BRANCH"
    echo $OUT

PS1="\u \$(git_prompt) \$ "


bro3886:~/devel$ bash
bro3886  $ cd
bro3886  $ cd devel/nlp/corpora
bro3886 @ nlp (master) $ 

Adapted from Display Git Branch or Tag Names in your Bash Prompt and Command Prompt Directory Styling.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.