Adding git branch on the Bash command prompt

前端 未结 13 2029
感情败类
感情败类 2020-11-30 16:15

I tried adding the git branch I\'m currently working on (checked-out) on the bash prompt without success.. (while keeping my current path which shows the active dir

相关标签:
13条回答
  • 2020-11-30 16:52

    If you use the fish shell its quite straight forward. fish is an interactive shell which comes with lots of goodies. You can install it using apt-get.

    sudo apt-get install fish
    

    you can then change the prompt setting using

    > fish_config 
    Web config started at 'http://localhost:8001/'. Hit enter to stop.
    Created new window in existing browser session.
    

    now go to http://localhost:8001/ open the prompt tab and choose the classic + git option

    Now click on the use prompt button and you are set.

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

    git 1.9.3 or later: use __git_ps1

    Git provides a shell script called git-prompt.sh, which includes a function __git_ps1 that

    prints text to add to bash PS1 prompt (includes branch name)

    Its most basic usage is:

    $ __git_ps1
    (master)
    

    It also takes an optional format string:

    $ __git_ps1 'git:[%s]'
    git:[master]
    

    How to Get It

    First, copy the file to somewhere (e.g. ~/.git-prompt.sh).

    Option 1: use an existing copy on your filesystem. Example (Mac OS X 10.15):

    $ find / -name 'git-prompt.sh' -type f -print -quit 2>/dev/null
    /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh
    

    Option 2: Pull the script from GitHub.

    Next, add the following line to your .bashrc/.zshrc:

    source ~/.git-prompt.sh
    

    Finally, 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)")]\$ '
    

    git < 1.9.3

    But note that only git 1.9.3 (May 2014) or later allows you to safely display that branch name(!)

    See commit 8976500 by Richard Hansen (richardhansen):

    Both bash and zsh subject the value of PS1 to parameter expansion, command substitution, and arithmetic expansion.

    Rather than include the raw, unescaped branch name in PS1 when running in two- or three-argument mode, construct PS1 to reference a variable that holds the branch name.

    Because the shells do not recursively expand, this avoids arbitrary code execution by specially-crafted branch names such as

    '$(IFS=_;cmd=sudo_rm_-rf_/;$cmd)'.
    

    What devious mind would name a branch like that? ;) (Beside a Mom as in xkcd)

    More Examples

    still_dreaming_1 reports in the comments:

    This seems to work great if you want a color prompt with xterm (in my .bashrc):

    PS1='\[\e]0;\u@\h: \w\a\]\n${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\03‌​3[01;34m\]\w\[\033[00m\]$(__git_ps1)\$ ' 
    

    Everything is a different color, including the branch.

    In in Linux Mint 17.3 Cinnamon 64-bit:

    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] \w\[\033[00m\]$(__git_ps1) \$ ' 
    
    0 讨论(0)
  • 2020-11-30 16:57

    I have tried a small script in python that goes in a bin folder.... 'gitprompt' file

    #!/usr/bin/env python
    import subprocess, os
    s = os.path.join(os.getcwd(), '.git')
    def cut(cmd):
        ret=''
        half=0
        record = False
        for c in cmd:
            if c == "\n":
                if not (record):
                    pass
                else:
                    break
            if (record) and c!="\n":
                ret = ret + c
            if c=='*':
                half=0.5
            if c==' ':
                if half == 0.5:
                    half = 1
            if half == 1:
                record = True
        return ret
    if (os.path.isdir(s)):
        out = subprocess.check_output("git branch",shell=True)
        print cut(out)
    else:
        print "-"
    

    Make it executable and stuff

    Then adjust the bash prompt accordingly like :

    \u:\w--[$(gitprompt)] \$ 
    
    0 讨论(0)
  • 2020-11-30 16:58

    1- If you don't have bash-completion ... : sudo apt-get install bash-completion

    2- Edit your .bashrc file and check (or add) :

    if [ -f /etc/bash_completion ]; then
      . /etc/bash_completion
    fi
    

    3- ... before your prompt line : export PS1='$(__git_ps1) \w\$ '
    (__git_ps1 will show your git branch)

    4- do source .bashrc

    EDIT :

    Further readings : Don’t Reinvent the Wheel

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

    Here is a simple clean version that I use: link

    0 讨论(0)
  • 2020-11-30 17:00

    Follow the steps as below: (Linux)

    Edit the file ~/.bashrc, to enter following lines at its end (In case, of Mac, file would be ~/.bash_profile)

    # Git branch in prompt.
    parse_git_branch() {
        git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
    }
    export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "
    

    Now, start the new terminal window, and try entering to any git-repo. The current branch would be shown, with the prompt.

    4 More Info - MAC/Linux

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