Adding git branch on the Bash command prompt

前端 未结 13 2030
感情败类
感情败类 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 17:02
    vim ~/.bash
    
    parse_git_branch() {
         git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
    }
    
    export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $"
    

    To reflect latest changes run following command

    source ~/.bashrc
    

    Output:-

    chandrakant@NDL41104 ~/Chandrakant/CodeBase/LaravelApp (development) $
    
    0 讨论(0)
  • 2020-11-30 17:03

    Follow the below steps to show the name of the branch of your GIT repo in ubuntu terminal:

    step1: open terminal and edit .bashrc using the following command.

    vi .bashrc

    step2: add the following line at the end of the .bashrc file :

    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\] $ "

    step3: source .bashrc in the root (home) directory by doing:

    /rootfolder:~$ source .bashrc

    Step4: Restart and open the terminal and check the cmd. Navigate to your GIt repo directory path and you are done. :)

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

    Here is how I configured the prompt to display Git status:

    Get git-prompt script:

    curl -o ~/.git-prompt.sh https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
    

    And customize your prompt adding the following code in your .bashrc file:

    # Load Git functions
    source ~/.git-prompt.sh
    
    # Syntactic sugar for ANSI escape sequences
    txtblk='\e[0;30m' # Black - Regular
    txtred='\e[0;31m' # Red
    txtgrn='\e[0;32m' # Green
    txtylw='\e[0;33m' # Yellow
    txtblu='\e[0;34m' # Blue
    txtpur='\e[0;35m' # Purple
    txtcyn='\e[0;36m' # Cyan
    txtwht='\e[0;37m' # White
    bldblk='\e[1;30m' # Black - Bold
    bldred='\e[1;31m' # Red
    bldgrn='\e[1;32m' # Green
    bldylw='\e[1;33m' # Yellow
    bldblu='\e[1;34m' # Blue
    bldpur='\e[1;35m' # Purple
    bldcyn='\e[1;36m' # Cyan
    bldwht='\e[1;37m' # White
    unkblk='\e[4;30m' # Black - Underline
    undred='\e[4;31m' # Red
    undgrn='\e[4;32m' # Green
    undylw='\e[4;33m' # Yellow
    undblu='\e[4;34m' # Blue
    undpur='\e[4;35m' # Purple
    undcyn='\e[4;36m' # Cyan
    undwht='\e[4;37m' # White
    bakblk='\e[40m'   # Black - Background
    bakred='\e[41m'   # Red
    badgrn='\e[42m'   # Green
    bakylw='\e[43m'   # Yellow
    bakblu='\e[44m'   # Blue
    bakpur='\e[45m'   # Purple
    bakcyn='\e[46m'   # Cyan
    bakwht='\e[47m'   # White
    txtrst='\e[0m'    # Text Reset
    
    # Prompt variables
    PROMPT_BEFORE="$txtcyn\u@\h $txtwht\w$txtrst"
    PROMPT_AFTER="\\n\\\$ "
    
    # Prompt command
    PROMPT_COMMAND='__git_ps1 "$PROMPT_BEFORE" "$PROMPT_AFTER"'
    
    # Git prompt features (read ~/.git-prompt.sh for reference)
    export GIT_PS1_SHOWDIRTYSTATE="true"
    export GIT_PS1_SHOWSTASHSTATE="true"
    export GIT_PS1_SHOWUNTRACKEDFILES="true"
    export GIT_PS1_SHOWUPSTREAM="auto"
    export GIT_PS1_SHOWCOLORHINTS="true"
    

    If you want to find out more, you can get all the dotfiles here: https://github.com/jamming/dotfiles

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

    At first, open your Bash Profile in your home directory. The easiest way to open & edit your bash_profile using your default editor.

    For example, I open it using the VS Code using this command: code .bash_profile.

    Then just paste the following codes to your Bash.

    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\] $ "
    

    The function

    parse_git_branch()

    will fetch the branch name & then through PS1 you can show it in your terminal.

    Here,

    \u = Username

    @ = Static Text

    \h = Computer Name

    \w = Current Directory

    $ = Static Text

    You can change or remove these variables for more customization.


    If you use Git for the first time in terminal or instantly after configuration, maybe sometimes you can not see the branch name.

    If you get this problem, don't worry. In that case, just make a sample repository and commit it after some changes. When the commit command will execute once, the terminal will find git branch from then.


    0 讨论(0)
  • 2020-11-30 17:16
    parse_git_branch() {
        git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
    }
    export PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]$(parse_git_branch)\n\$ '
    
    0 讨论(0)
  • 2020-11-30 17:16
    root:~/project#  -> root:~/project(dev)# 
    

    add the following code to the end of your ~/.bashrc

    force_color_prompt=yes
    color_prompt=yes
    parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
    }
    if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
    else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
    fi
    unset color_prompt force_color_prompt
    
    0 讨论(0)
提交回复
热议问题