在终端中预先安装当前的git分支

如何配置终端显示当前的git分支?

我想看到第二行而不是第一行:

andy@bob:~/my_projects/project_x$ (master)~/my_projects/project_x$ 

我不想运行git status来查看我目前在哪个分支!

您可以将以下代码添加到.bashrc文件中:

 parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' } PS1="${debian_chroot:+($debian_chroot)}\u@\h:\w\$(parse_git_branch) $ " 

您可以移动这些组件来配置您的口味,例如前置$(parse_git_branch)而不显示我使用的用户@计算机部件:

 PS1="\$(parse_git_branch)${debian_chroot:+($debian_chroot)}\w$ " 

哪个显示:

 (master)~/my_projects/project_x$ 

另请参见: .bash_profile文件中此PS1变量的作用是什么?

将这一个衬里添加到.bashrc

 export PS1='\u@\h \W$(__git_ps1 " [ - %s - ]") \$ ' 

您还可以将以下行添加到.bashrc:

 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\`__git_ps1`\$ ' 

这将为终端中的分支名称添加颜色

 git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' } PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[1;31m\]'"\$(git_branch)\[\033[00m\]$ "