Can I display GIT in prompt when current directory has/contains a .git folder? Is there a way to do this? My current prompt is defined like so:

export PS1="[\u@\h] \w $ "

So, my prompt looks like this:

[user@computer] ~/workspace  $

And I want it to dynamically look like this:

[user@computer] ~/workspace GIT $
  • 1
    You might be interested in this GitHub repository and the various related forks.
    – Wildcard
    Apr 21, 2016 at 20:41
  • A simpler workaround might be to use your shell's prompt-command feature to reset PS1 appropriately
    – Jeff Schaller
    Apr 21, 2016 at 20:55
  • @Wildcard I use that repo! I use cat @ mint-kitty : ~ $ normally, and cat @ mint-kitty : ~/repo(branch??!) $ when I'm on a branch.
    – cat
    Apr 21, 2016 at 23:55
  • One neat bash prompt customization for git that I've seen is to display time since last commit along with the branch information. Apr 22, 2016 at 5:10

5 Answers 5


The most standard way is to use __git_ps1 directly from git. In Ubuntu, it is available in this path:

source /usr/lib/git-core/git-sh-prompt
## source /etc/bash_completion.d/git-prompt
#PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w $(__git_ps1 "(%s)")\$ '

You can notice the added part $(__git_ps1 "(%s)"), which will notify you about the current state of repo -- current branch, ongoing rebases, merges and so on.

The file in Ubuntu is provided by git package:

$ dpkg-query -S /usr/lib/git-core/git-sh-prompt
git: /usr/lib/git-core/git-sh-prompt

For fedora by git-core (with a bit different path):

rpm -qf /usr/share/git-core/contrib/completion/git-prompt.sh

Your prompt will change from

[user@computer] ~/workspace $ 


[user@computer] ~/workspace (master)$ 
  • This file doesn't exist on my Ubuntu box (although the directory does, and it contains a lot of files.)
    – Wildcard
    Apr 22, 2016 at 0:23
  • @Wildcard It probably depends on version. On my 15.10 it is provided by git package (as noted in modified answer).
    – Jakuje
    Apr 22, 2016 at 6:14

You would probably be happier with this: is there a .git folder here or in one of my parent directories?

in_git () { 
    local dir=$PWD
    until [[ $dir == / ]]; do 
        [[ -d "$dir/.git" ]] && return 0
        dir=$(dirname "$dir")
    return 1

Then your prompt is

PS1='[\u@\h] \w $(in_git && echo "GIT ")\$ '                                 

This will show the GIT indicator if you're in some subdirectory in your repo.

  • True enough, but then you might as well just use git status >/dev/null 2>&1 and see if you get an error, and at that point you're just reinventing the wheel.
    – Wildcard
    Apr 21, 2016 at 21:34
  • 1
    Also this will fail if you're inside a .git directory; it will falsely report that you're in a git repository even though many git commands will not work other than in the working tree.
    – Wildcard
    Apr 21, 2016 at 21:35
  • @Wildcard Many commands that do not require a working tree will work, though, and it is useful to recognize bare repos. It is most certainly not a "false" report (well, maybe if you're deep inside the bare repo, but not immediately inside).
    – jpmc26
    Apr 22, 2016 at 2:22
  • 1
    @Wildcard git is not really a system where you want to be blissfully unaware of the internals. It does not do an excellent job of isolating you from the plumbing. This has benefits and drawbacks, but regardless of whether it's good or bad, it is a reality. There's nothing misleading about it if you have a rudimentary understanding of where git puts things and how bare repos differ from non-bare repos.
    – jpmc26
    Apr 22, 2016 at 4:00
  • 1
    @jpmc26, fair enough. That's not the only issue here; there are even cases wherein .git can be a regular file rather than a directory. As I mentioned in my answer, the [ -d .git ] test is only mostly correct. (NB: My answer has those issues also, but the git repo I linked handles these cases much better.)
    – Wildcard
    Apr 22, 2016 at 4:02

Thanks for all your answers! I ended up up putting this in my .bash_profile (on Mac):

parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
  1. Change your PROMPT_COMMAND as follows:

    PROMPT_COMMAND="$PROMPT_COMMAND; if [ -d .git ]; then ingitrepo='GIT '; else ingitrepo=''; fi"
  2. Change your PS1 as follows:

    PS1='[\u@\h] \w ${ingitrepo}\$ '

Note that single-quotes are used when setting PS1 to prevent premature variable resolution, and also note that a backslash before $ at the end of your PS1 is recommended—it's the same as $ unless you are root; then it becomes #.

Also note that [ -d .git ] is only mostly a valid test for whether you're in a git repository. But for quick-and-dirty, and for your own personal use, it's not bad.


You can install https://github.com/nojhan/liquidprompt Which will show dev ± where ± means git and dev is the git branch you are currently in.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .