Git completion and PS1 not working: "__git_ps1: command not found" on "sudo -s" or "sudo su" on Ubuntu 12.04

Chris Cinelli picture Chris Cinelli · Jul 26, 2012 · Viewed 22k times · Source

I installed git and git-flow completion adding these line to .bashrc of root and a normal_user on a Ubuntu 12.04 machine:

source /etc/git-completion.bash
source /etc/git-flow-completion.bash
GIT_PS1_SHOWUPSTREAM="verbose"
GIT_PS1_SHOWDIRTYSTATE=true
PS1='\[\033[32m\]\u@\h\[\033[00m\]:\[\033[34m\]\w\[\033[31m\]$(__git_ps1)\[\033[00m\]\$ '

When I log as root or normal_user git completion works. However if I use "sudo -s" or "sudo su" git completion is not working and I continually get "__git_ps1: command not found" each time I press return. I tried to remove the "source" commands and use "apt-get install bash-completion" (bash-completion was already installed). So even without the 2 source I get the exact same behavior.

Anybody knows what the problem is and how to make it work?

Answer

Peter van der Does picture Peter van der Does · Jul 26, 2012

When you do sudo su it won't source the users .bashrc. The PS1 is inherited from the user you did the sudo su from but the new shell doesn't know where it can find ___git_ps1

You need to simulate a login by executing sudo su -l