-bash: __git_ps1: command not found
Asked Answered
O

8

84

I tried to install Ruby 2.0. My command line urped and now looks like the following:

-bash: __git_ps1: command not found
[11:58:28][whatever@whatever ~]$ 

I have not a clue how to get rid of the __git_ps1: command not found error. I've searched my .bash_profile and my .bashrc to see if it's trying to set a variable or something and am not seeing anything. The only place I can find git_ps1 mentioned is in ~/.dotfiles/.bash_prompt. I replace the content of that file completely, logout and log back in and it fixes nothing.

I saw this, but I'm pretty new to command line so I just confused myself.

Any ideas?

Oppenheimer answered 13/3, 2013 at 11:28 Comment(4)
which version of git are you using?Incomprehensive
take a look bbs.archlinux.org/viewtopic.php?id=147462Incomprehensive
git version 1.7.10.2 (Apple Git-33)Oppenheimer
RESOLVED AT: gist.github.com/scottious/0d99ea77daa041b28929Schargel
C
33

BASH has a whole slew of ways of automatically setting your prompt to give you nice information. You set the prompt by setting the PS1 environment variable. For example, if I set PS1="$ " my prompt will look like this:

$ 

Not too informative. All I can tell is that the command line is prompting me.

However, If I set PS1=\u@\h: \w$, my prompt will now look like this:

david@vegibank:/usr/bin$ 

That tells me how I'm logged in (the \u), the machine I'm onto (\h), and the directory I'm in (the \w). If I use git, it would be nice if the git branch I'm in is also part of my prompt.

This is exactly what is going on with your .profile, your .bashrc file, your .bash_login or your .bash_profile script. Or, what some system admin did in /etc/profile.

There are a couple things you can do. Either:

  • Download the missing __git_ps1 and make sure it's in your $PATH environment variable (which is set by a combination of the various initialization files mentioned above)
  • Change your PS1 environment variable in whatever initialization file is being executed (I believe it is probably .bash_profile.

Just add this as the last line:

PS1="\u@\h:\w\n$ "

The added \n prints the dollar sign prompt on the line below like this:

david@vegibank:/usr/bin
$ 

I like to do that because the prompt can get rather long and editing the command line gets tricky when the prompt is longer than 30 to 50 characters. Otherwise, it's pretty much the standard prompt that most users use. You can see more about setting BASH prompts in the man pages. (Search for the word Prompting on that page).

If you find it a bit confusing, be glad you're not using Kornshell. I use Kornshell and to get the same prompt PS1=\u@\h:\w\n$ does, I set my prompt as:

export PS1='$(print -n "`logname`@`hostname`:";if [[ "${PWD#$HOME}" != "$PWD" ]] then; print -n "~${PWD#$HOME}"; else; print -n "$PWD";fi;print "\n$ ")'
Considered answered 14/3, 2013 at 4:21 Comment(4)
I added this to the last line of my BP and it's at least not breaking now. Will keep trying to figure out why I can't curl the below and make that work, too. Thanks all.Oppenheimer
Just go into Git and manually download the file. Go to github.com/git/git/blob/master/contrib/completion/git-prompt.sh in a browser, and click on the Raw button. Then cut and paste.Considered
The question was about "__git_ps1" that is declared in git-prompt.sh. The @emil-lundberg answer is the right answerGrettagreuze
@Grettagreuze Actually, the question was about the command not found error in the OP's messed up prompt, and how to get rid of it.Considered
F
156

Run the following:

$ curl -L https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh > ~/.bash_git

And add this to the top of your ~/.bashrc:

source ~/.bash_git

Re-login to your shell and you should be set.

Frigorific answered 13/3, 2013 at 22:59 Comment(4)
Blake, I get this: curl: (3) <url> malformedOppenheimer
If you have git installed locally (at least on Fedora), the git-prompt.sh script is included with the git distribution at /usr/share/git-core/contrib/completion/git-prompt.shOlnay
@AlexJordan much the same holds under Arch: /usr/share/git/completion/git-prompt.shClein
On Debian Jessie it's called git-sh-prompt and is at /usr/lib/git-core/git-sh-promptTeens
C
60

Search your system for a git-prompt.sh, you need to source that for the __git_ps1 function to be available. In Arch, it is currently located at /usr/share/git/completion/git-prompt.sh. Add

source /path/to/git-prompt.sh

to some suitable shell script. If you're unsure where, add it to your ~/.bashrc.

If you have locate installed, you can use that to find the git-prompt.sh file, but you may need to run updatedb as root first.

Clein answered 7/7, 2013 at 1:8 Comment(3)
This is the GOOD answer, add this in ~/.bashrc BEFORE the PS1 export "source /usr/share/git-core/contrib/completion/git-prompt.sh" without quotesGrettagreuze
This worked on debian stretch, i installed git-core and i had no git-prompt.sh or completion folder.Komsa
For Ubuntu 20.04, the line is source /etc/bash_completion.d/git-prompt.Supersensitive
C
33

BASH has a whole slew of ways of automatically setting your prompt to give you nice information. You set the prompt by setting the PS1 environment variable. For example, if I set PS1="$ " my prompt will look like this:

$ 

Not too informative. All I can tell is that the command line is prompting me.

However, If I set PS1=\u@\h: \w$, my prompt will now look like this:

david@vegibank:/usr/bin$ 

That tells me how I'm logged in (the \u), the machine I'm onto (\h), and the directory I'm in (the \w). If I use git, it would be nice if the git branch I'm in is also part of my prompt.

This is exactly what is going on with your .profile, your .bashrc file, your .bash_login or your .bash_profile script. Or, what some system admin did in /etc/profile.

There are a couple things you can do. Either:

  • Download the missing __git_ps1 and make sure it's in your $PATH environment variable (which is set by a combination of the various initialization files mentioned above)
  • Change your PS1 environment variable in whatever initialization file is being executed (I believe it is probably .bash_profile.

Just add this as the last line:

PS1="\u@\h:\w\n$ "

The added \n prints the dollar sign prompt on the line below like this:

david@vegibank:/usr/bin
$ 

I like to do that because the prompt can get rather long and editing the command line gets tricky when the prompt is longer than 30 to 50 characters. Otherwise, it's pretty much the standard prompt that most users use. You can see more about setting BASH prompts in the man pages. (Search for the word Prompting on that page).

If you find it a bit confusing, be glad you're not using Kornshell. I use Kornshell and to get the same prompt PS1=\u@\h:\w\n$ does, I set my prompt as:

export PS1='$(print -n "`logname`@`hostname`:";if [[ "${PWD#$HOME}" != "$PWD" ]] then; print -n "~${PWD#$HOME}"; else; print -n "$PWD";fi;print "\n$ ")'
Considered answered 14/3, 2013 at 4:21 Comment(4)
I added this to the last line of my BP and it's at least not breaking now. Will keep trying to figure out why I can't curl the below and make that work, too. Thanks all.Oppenheimer
Just go into Git and manually download the file. Go to github.com/git/git/blob/master/contrib/completion/git-prompt.sh in a browser, and click on the Raw button. Then cut and paste.Considered
The question was about "__git_ps1" that is declared in git-prompt.sh. The @emil-lundberg answer is the right answerGrettagreuze
@Grettagreuze Actually, the question was about the command not found error in the OP's messed up prompt, and how to get rid of it.Considered
U
7

Quoting from /usr/lib/git-core/git-sh-prompt:

# This script allows you to see repository status in your prompt.
#
# To enable:
#
#    1) Copy this file to somewhere (e.g. ~/.git-prompt.sh).
#    2) Add the following line to your .bashrc/.zshrc:
#        source ~/.git-prompt.sh
#    3a) Change your PS1 to call __git_ps1 as
#        command-substitution:
#        Bash: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
#        ZSH:  setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
#        the optional argument will be used as format string.
#    3b) Alternatively, for a slightly faster prompt, __git_ps1 can
#        be used for PROMPT_COMMAND in Bash or for precmd() in Zsh
#        with two parameters, <pre> and <post>, which are strings
#        you would put in $PS1 before and after the status string
#        generated by the git-prompt machinery.  e.g.
#        Bash: PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ "'
#          will show username, at-sign, host, colon, cwd, then
#          various status string, followed by dollar and SP, as
#          your prompt.
#        ZSH:  precmd () { __git_ps1 "%n" ":%~$ " "|%s" }
#          will show username, pipe, then various status string,
#          followed by colon, cwd, dollar and SP, as your prompt.
#        Optionally, you can supply a third argument with a printf
#        format string to finetune the output of the branch status

Following these steps should fix your issue!!

Udine answered 21/3, 2019 at 6:25 Comment(0)
A
6

As of 2019, the prompt helper function should be installed when installing the git package and can be found at /usr/lib/git-core/git-sh-prompt.

If it is not loaded, install bash-completion package and have a look into your ~/.bashrc.

In my case I had to uncomment this:

if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    . /etc/bash_completion
fi

and open a new shell, and all was well.

Root cause was 'bash-completion` not being set up properly in the first place on a fresh install.

Assr answered 9/3, 2019 at 21:20 Comment(0)
E
2

I solved it simply using

sudo apt install git

This appears because git is not installed and therefore the environment variable is not defined.

Evilminded answered 27/5, 2020 at 20:12 Comment(0)
C
1

Running source /usr/share/bash-completion/completions/git worked for me on Ubuntu-like distro with git version 2.17.1.

Cassiecassil answered 19/4, 2022 at 8:26 Comment(0)
F
-1

Remove gitfast plugin from ~/.zshrc

+ plugins=(git vscode laravel emoji timer zsh-autosuggestions gitfast)
- plugins=(git vscode laravel emoji timer zsh-autosuggestions)
Fluffy answered 15/9, 2022 at 19:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.