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

I installed git and git -flow termination by adding this line to the .bashrc root and 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 register as shutdown root or normal_user git. However, if I use "sudo -s" or "sudo su" git, the termination does not work, and I constantly get "__git_ps1: command not found" every time I press return. I tried to remove the "source" commands and use "apt-get install bash -completion" (bash -completion is already installed). So even without source 2, I get the same behavior.

Does anyone know what the problem is and how to make it work?

+9
git bash bash-completion
Jul 26 '12 at 5:12
source share
6 answers

When you run sudo su , it will not be sent to .bashrc users. PS1 is inherited from the user you made sudo su , but the new shell does not know where it can find ___git_ps1

You need to simulate the login by running sudo su -l

+14
Jul 26 2018-12-12T00:
source share

In your case, this is due to the fact that the git -prompt.sh file did not start when the terminal started, in the git -core source files you can find contrib/completion/git-prompt.sh .

Perhaps a machine is already present, to search for:

Find ~ -name git -prompt.sh

It can take a lot of time and, therefore, it is better to specify more precisely instead of / search, perhaps you will guess where it can be found. When you find it, add to the .bashrc before the expression the expression promt in an example, as I did with an indication of the ways:

if [-f $ HOME / git / 1.8.0 / contrib / completion / git -prompt.sh]; then

. $ HOME / git / 1.8.0 / contrib / completion / git -prompt.sh

c

Finally:

. ~ / .bashrc

+5
Nov 11
source share

The functionality of the invitation was split into git -completion.bash on git -prompt.sh on May 22, 2012 ; you will need the same source . Git 1.7.12 was the first release to see this change. I had the same problem when updating my git -completion.bash.

+4
May 30 '13 at 20:18
source share

Assuming you won’t have git shutdown when you log in to root via sudo su , a little bit of bash kung fu so as not to try to evaluate __git_ps1 .

At the PS1 prompt, you can put any conditional expressions (therefore, how they can be replaced in the branch name when in the git directory). So, just wrap the git stuff in conditional check, you are not user id 0 ( root ).

Replace in your export expression PS1 :

 $(__git_ps1) 

from

 $(if [ $(id -u) -ne 0 ]; then echo $(__git_ps1) ; fi) 

Now the entire invitation to the OP will look like this:

  PS1='\[\033[32m\]\u@\h\[\033[00m\]:\[\033[34m\]\w\[\033[31m\]$(if [ $(id -u) -ne 0 ]; then echo $(__git_ps1) ; fi)\[\033[00m\]\$ ' 

Now in the shell, you can sudo su without reporting an error.

+1
Feb 28 '17 at 0:58
source share

If you prefer not to add additional flags such as -l (or don’t want the alias su, etc.), you can also just change the root bascc to not use __git_ps1 .

For example, in my /root/.bashrc I have (I like the root to be red):

 export PS1='\[\e[1;31m\][\u@\h \W]# \[\e[0m\]' 

Basically, just copy the PS1 you have in ~/.bashrc or similar /root/.bashrc and delete any links to __git_ps1.

Ideally, you rarely run development as root, so __git_ps1 not needed. If you do this (not recommended), you can copy all the code necessary to execute __git_ps1 to /root/.bashrc .

0
Oct 05 '15 at 16:50
source share

Perhaps a little late, however you can replace in your PS1:

 (__git_ps1) 

from

 (type -t __git_ps1 >& /dev/null && __git_ps1) 

This will disable the __git_ps1 call when it is unavailable, which you probably will not need as root.

-one
Mar 04 '15 at 17:40
source share



All Articles