(Mac) -bash: __git_ps1: command not found

后端 未结 19 1924
感动是毒
感动是毒 2020-11-30 16:20

I\'m trying to change my command promt in terminal. I keep getting the error:

-bash: __git_ps1: command not found

I\'ve tried it just by typing

相关标签:
19条回答
  • 2020-11-30 16:48

    I was doing the course on Udacity for git hub and was having this same issue. Here is my final code that make is work correctly.

    # Change command prompt
    alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \ .   (.*\)/(\1)/'"
    
    if [ -f ~/.git-completion.bash ]; then
      source ~/.git-completion.bash
      export PS1='[\W]$(__git_ps1 "(%s)"): '
    fi
    
    source ~/.git-prompt.sh
    export GIT_PS1_SHOWDIRTYSTATE=1
    # '\u' adds the name of the current user to the prompt
    # '\$(__git_ps1)' adds git-related stuff
    # '\W' adds the name of the current directory
    export PS1="$purple\u$green\$(__git_ps1)$blue \W $ $reset"
    

    It works! https://i.stack.imgur.com/d0lvb.jpg

    0 讨论(0)
  • 2020-11-30 16:49

    After upgrading to OSX 10.9 Mavericks I had to reference the following files to get git shell command completion and git prompt to work again.

    From my .bash_profile or similar:

    if [ -f /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash ]; then
        . /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash
    fi
    
    source /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh
    
    #shell prompt example
    PS1='\u $(__git_ps1 "(%s)")\$ '
    
    0 讨论(0)
  • 2020-11-30 16:49

    Following worked for me like a charm:

    Run following in your Terminal:

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

    Open/Create bash_profile:

    $ vi ~/.bash_profile
    

    Add following to the file:

    source ~/.bash_git
    export PS1='\[\033[01;32m\]os \[\033[01;34m\]\w $(__git_ps1 "[%s]")\$\[\033[00m\] '
    export GIT_PS1_SHOWDIRTYSTATE=1
    export GIT_PS1_SHOWUPSTREAM="auto"
    

    Finally, source it using:

    $ source ~/.bash_profile
    

    This will solve the problem of bash: __git_ps1: command not found.

    Also your prompt will change to "os ". To change "os" to something else, modify "os" string in export PS1 line.

    0 讨论(0)
  • 2020-11-30 16:50

    Solution for MacOS Sierra and git version 2.10.1 <2017-2-06>

    Step 1: Install the Git

    You can skip this step if you already installed the latest git.

    Download git package from browser https://git-scm.com/download/

    Note: if you install with curl [option] https://... option to download, you would have to make sure your system support SSL. So for new comer, to download from browser and install directly from git installer is much easier.

    Installation Verification:
    • Show where is your git directory at: which git
    • Show which version your git currently is: git --version current version should be 2.10.1.

    Step 2: Add your git profile to your shell

    1. Open your shell profile:
      • nano ~/.bash_profile or nano ~/.bashrc Depends on where your modification is.
    2. Add the following code to the file:
      • source /usr/local/git/contrib/completion/git-completion.bash
      • source /usr/local/git/contrib/completion/git-prompt.sh

    Note: git installation location changed from opt/ directory to usr/local/ after OSX upgrade to El Capitain, and this is why some of the old answer above doesn't work anymore in MacOS Sierra.

    1. Add the following code to your PS1 configuration:

      • Option 1: add directly to your PS1: export PS1='\w$(__git_ps1 "(%s)") > '

        I prefer this simple approach since I already know the .git-completion.bash is there in my home directory, and I can add other prompt format in the front of it. here is my personal prompt for your reference: export PS1='\t H#\! \u:\w$(__git_ps1 "{%s}") -->> '
      • Option 2: Add a selection script

      if [ -f ~/.git-completion.bash ]; then
            export PS1='\w$(__git_ps1 "(%s)") > '
      fi
    2. Save and use the profile: source ~/.bash_profile or source ~/.bashrc

    Now you should see the git prompt working properly and shows which branch you are in right now.
    0 讨论(0)
  • 2020-11-30 16:52

    __git_ps1 for bash is now found in git-prompt.sh in /usr/local/etc/bash_completion.d on my brew installed git version 1.8.1.5

    0 讨论(0)
  • 2020-11-30 16:52

    I know it's not a real answer...

    I had some strange issues with sourcing git-prompt.sh in my .bashrc so I started to look for other solution. This one: http://www.jqno.nl/post/2012/04/02/howto-display-the-current-git-branch-in-your-prompt/ doesn't use __git_ps1 and author claims it works also on Mac (for now it works perfectly on my Ubuntu and it's easy to tweak).

    I hope it helps!

    0 讨论(0)
提交回复
热议问题