(Mac) -bash: __git_ps1: command not found

后端 未结 19 1938
感动是毒
感动是毒 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: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.

提交回复
热议问题