在 Bash 命令提示符下添加 git branch
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15883416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding git branch on the Bash command prompt
提问by George Katsanos
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 directory/fileintact) I have a .bashrc file on my home, but I also saw many people mentioning the .profile file..
我尝试在 bash 提示符下添加我目前正在处理(签出)的 git 分支,但没有成功..(同时保持显示活动目录/文件完整的当前路径)我家中有一个 .bashrc 文件,但我也看到很多人提到 .profile 文件..
采纳答案by VonC
git 1.9.3 or later: use __git_ps1
git 1.9.3 或更高版本:使用 __git_ps1
Git provides a shell script called git-prompt.sh
, which includes a function __git_ps1
that
GIT中提供称为外壳脚本git-prompt.sh
,其包括功能__git_ps1
在于
prints text to add to bash PS1 prompt (includes branch name)
打印文本以添加到 bash PS1 提示符(包括分支名称)
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
).
首先,将文件复制到某个地方(例如~/.git-prompt.sh
)。
Option 1: use an existing copy on your filesystem. Example (Mac OS X 10.15):
选项 1:使用文件系统上的现有副本。示例(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.
选项 2:从 GitHub拉取脚本。
Next, add the following line to your .bashrc/.zshrc
:
接下来,将以下行添加到您的.bashrc/.zshrc
:
source ~/.git-prompt.sh
Finally, change your PS1
to call __git_ps1
as command-substitution:
最后,将您PS1
的调用更改__git_ps1
为命令替换:
Bash:
重击:
PS1='[\u@\h \W$(__git_ps1 " (%s)")]$ '
Zsh:
Zsh:
setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 " (%s)")]$ '
git < 1.9.3
git < 1.9.3
But note that only git 1.9.3 (May 2014) or laterallows you to safelydisplay that branch name(!)
但请注意,只有 git 1.9.3(2014 年 5 月)或更高版本允许您安全地显示该分支名称(!)
See commit 8976500by Richard Hansen (richardhansen
):
见犯8976500由理查德·汉森(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, constructPS1
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
bash 和 zsh 都使 PS1 的值受参数扩展、命令替换和算术扩展的影响。
在
PS1
以二或三参数模式运行时,不要包含原始的、未转义的分支名称,而是构造PS1
以引用保存分支名称的变量。由于 shell 不会递归扩展,这避免了通过特制的分支名称执行任意代码,例如
'$(IFS=_;cmd=sudo_rm_-rf_/;$cmd)'.
What devious mind would name a branch like that? ;) (Beside a Mom as in xkcd)
哪个狡猾的头脑会这样命名一个分支?;)(在 xkcd 中的妈妈旁边)
More Examples
更多例子
still_dreaming_1reports in the comments:
This seems to work great if you want a color prompt with
xterm
(in my.bashrc
):
如果您想要带有
xterm
(在我的.bashrc
)中的颜色提示,这似乎很有效:
PS1='\[\e]0;\u@\h: \w\a\]\n${debian_chroot:+($debian_chroot)}\[3[01;32m\]\u@\h\[3[00m\]:\[??3[01;34m\]\w\[3[00m\]$(__git_ps1)$ '
Everything is a different color, including the branch.
一切都是不同的颜色,包括树枝。
In in Linux Mint 17.3 Cinnamon 64-bit:
在 Linux Mint 17.3 Cinnamon 64 位中:
PS1='${debian_chroot:+($debian_chroot)}\[3[01;32m\]\u@\h\[3[01;34m\] \w\[3[00m\]$(__git_ps1) $ '
回答by parasrish
Follow the steps as below: (Linux)
请按照以下步骤操作:(Linux)
Edit the file ~/.bashrc
, to enter following lines at its end (In case, of Mac, file would be ~/.bash_profile
)
编辑文件~/.bashrc
,在其末尾输入以下行(如果是 Mac,文件将为~/.bash_profile
)
# Git branch in prompt.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ ()/'
}
export PS1="\u@\h \W\[3[32m\]$(parse_git_branch)\[3[00m\] $ "
Now, start the new terminal window, and try entering to any git-repo. The current branch would be shown, with the prompt.
现在,启动新的终端窗口,并尝试进入任何 git-repo。将显示当前分支,并带有提示。
回答by Eric Lavoie
1- If you don't have bash-completion... : sudo apt-get install bash-completion
1- 如果您没有bash-completion... :sudo apt-get install bash-completion
2- Edit your .bashrcfile and check (or add) :
2- 编辑您的.bashrc文件并检查(或添加):
if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
3- ... before your prompt line : export PS1='$(__git_ps1) \w\$ '
(__git_ps1will show your git branch)
3- ...你迅速前行:export PS1='$(__git_ps1) \w\$ '
(__git_ps1会显示您的Git分支)
4- do source .bashrc
4-做 source .bashrc
EDIT :
编辑 :
Further readings : Don't Reinvent the Wheel
进一步阅读:不要重新发明轮子
回答by jaguililla
Here is how I configured the prompt to display Git status:
以下是我如何配置提示以显示 Git 状态:
Get git-prompt script:
获取 git-prompt 脚本:
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:
并自定义您的提示,在您的 .bashrc 文件中添加以下代码:
# 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
如果您想了解更多信息,可以在此处获取所有 dotfile:https: //github.com/jamming/dotfiles
回答by Mike Chen
For mac, this works really well: http://martinfitzpatrick.name/article/add-git-branch-name-to-terminal-prompt-mac/:
对于 mac,这非常有效:http: //martinfitzpatrick.name/article/add-git-branch-name-to-terminal-prompt-mac/:
# Git branch in prompt.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ ()/'
}
export PS1="\u@\h \W\[3[32m\]$(parse_git_branch)\[3[00m\] $ "
回答by IFTEKHAR I ASIF
At first, open your Bash Profile in your home directory. The easiest way to open & edit your bash_profileusing your default editor.
首先,在您的主目录中打开您的 Bash 配置文件。使用默认编辑器打开和编辑bash_profile的最简单方法。
For example, I open it using the VS Code using this command: code .bash_profile.
例如,我使用 VS Code 使用以下命令打开它:code .bash_profile。
Then just paste the following codes to your Bash.
然后只需将以下代码粘贴到您的 Bash 中。
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ ()/'
}
export PS1="\u@\h \W\[3[32m\]$(parse_git_branch)\[3[00m\] $ "
The function
功能
parse_git_branch()
parse_git_branch()
will fetch the branch name & then through PS1 you can show it in your terminal.
将获取分支名称,然后通过 PS1 您可以在终端中显示它。
Here,
这里,
\u = Username
@ = Static Text
\h = Computer Name
\w = Current Directory
$ = Static Text
\u = 用户名
@ = 静态文本
\h = 计算机名称
\w = 当前目录
$ = 静态文本
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.
如果您是第一次在终端中使用 Git 或在配置后立即使用 Git,有时您可能看不到分支名称。
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.
如果您遇到此问题,请不要担心。在这种情况下,只需创建一个示例存储库并在进行一些更改后提交即可。当 commit 命令执行一次时,终端会从中找到 git branch。
回答by Chandrakant Ganji
vim ~/.bash
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ ()/'
}
export PS1="\u@\h \[3[32m\]\w\[3[33m\]$(parse_git_branch)\[3[00m\] $"
To reflect latest changes run following command
要反映最新的更改,请运行以下命令
source ~/.bashrc
Output:-
输出:-
chandrakant@NDL41104 ~/Chandrakant/CodeBase/LaravelApp (development) $
回答by Ali Alp
root:~/project# -> root:~/project(dev)#
add the following code to the end of your ~/.bashrc
将以下代码添加到 ~/.bashrc 的末尾
force_color_prompt=yes
color_prompt=yes
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/()/'
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[3[01;32m\]\u@\h\[3[00m\]:\[3[01;34m\]\w\[3[01;31m\]$(parse_git_branch)\[3[00m\]$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)$ '
fi
unset color_prompt force_color_prompt
回答by Shasak
If you use the fish shellits quite straight forward.
fish is an interactive shell which comes with lots of goodies. You can install it using apt-get
.
如果你使用鱼壳,它很简单。鱼是一个交互式外壳,带有很多好东西。您可以使用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
现在去http://localhost:8001/
打开提示选项卡并选择经典 + git 选项
Now click on the use prompt button and you are set.
现在点击使用提示按钮,你就设置好了。