我尝试在bash提示符上添加我目前正在工作的git分支(签出),但没有成功。(同时保持我的当前路径显示活动目录/文件完整) 我有一个。bashrc文件在我的家,但我也看到很多人提到。profile文件…
1-如果你没有bash-completion…: sudo apt-get install bash-completion
2-编辑你的.bashrc文件并检查(或添加):
if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
3 -…export PS1='$(__git_ps1) \w\$ ' (__git_ps1将显示你的git分支)
4- do source .bashrc
编辑:
进一步阅读:不要重新发明轮子
Git 1.9.3或更高版本:使用__git_ps1
Git提供了一个名为Git -prompt.sh的shell脚本,其中包含一个函数__git_ps1
打印文本添加到bash PS1提示符(包括分支名称)
它最基本的用法是:
$ __git_ps1
(master)
它还接受一个可选的格式字符串:
$ __git_ps1 'git:[%s]'
git:[master]
如何得到它
首先,将文件复制到某个地方(例如~/.git-prompt.sh)。
选项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
选项2:从GitHub中提取脚本。
接下来,添加以下行到你的.bashrc/.zshrc:
source ~/.git-prompt.sh
最后,改变你的PS1,调用__git_ps1作为命令替换:
Bash:
PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
Zsh:
setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
git < 1.9.3
但请注意,只有git 1.9.3(2014年5月)或更高版本才允许安全地显示该分支名称(!)
参见Richard Hansen的commit 8976500 (richardhansen):
bash和zsh都使PS1的值服从参数展开、命令替换和算术展开。
而不是包括原始的,未转义的分支名称在PS1运行时,二或 三参数模式,构造PS1来引用保存分支名称的变量。
因为shell不会递归地展开,这就避免了通过专门制作的分支名称来执行任意代码,例如
'$(IFS=_;cmd=sudo_rm_-rf_/;$cmd)'.
哪个狡猾的人会给分支起这样的名字?;)(在xkcd的妈妈旁边)
更多的例子
Still_dreaming_1在评论中报道:
如果你想用xterm(在我的.bashrc中)显示颜色提示,这似乎很管用:
PS1='\[\e]0;\u@\h: \w\a\]\n${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1)\$ '
所有的东西都是不同的颜色,包括树枝。
在Linux Mint 17.3肉桂64位:
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] \w\[\033[00m\]$(__git_ps1) \$ '
正如J'e在评论中指出的那样
Ubuntu 修改PS1作业与 PS1 = {debian_chroot美元:+ (debian_chroot)}[\ 033美元[01;32m [h] u@ \ \ \ 033[米]:[\ 033 [01;34m] \ w[033[米\]\ 033美元[0;32m (__git_ps1”(% s”)033 [0m \美元” #——-——- PS1 = {debian_chroot美元:+ (debian_chroot美元)的u@ \ h: \ w美元(__git_ps1”(% s”)\美元
下面是我如何配置提示符来显示Git状态:
获取git-prompt脚本:
curl -o ~/.git-prompt.sh https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
并自定义提示符,在.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"
如果你想了解更多,你可以在这里获得所有的dotfiles: https://github.com/jamming/dotfiles
我尝试了一个python小脚本,放在....文件夹中 “gitprompt”文件
#!/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 "-"
让它可执行之类的
然后相应地调整bash提示符,如下:
\u:\w--[$(gitprompt)] \$
对于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/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "
请按照以下步骤操作:(Linux)
编辑文件~/。bashrc,在它的末尾输入以下行(在Mac中,文件将是~/.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\] $ "
现在,启动新的终端窗口,并尝试进入任何git-repo。将显示当前分支,并显示提示符。
4更多信息- MAC/Linux
如果你用的是鱼壳,那就很直接了。 Fish是一个互动的贝壳,里面有很多好吃的东西。您可以使用apt-get安装它。
sudo apt-get install fish
然后,您可以使用
> fish_config
Web config started at 'http://localhost:8001/'. Hit enter to stop.
Created new window in existing browser session.
现在登录http://localhost:8001/ 打开提示选项卡并选择经典的+ git选项
现在点击使用提示按钮,你就设置好了。
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\$ '
按照以下步骤在ubuntu终端中显示你的GIT repo分支的名称:
步骤1:打开终端,使用下面的命令编辑.bashrc。
vi . bashrc
步骤2:在.bashrc文件的末尾添加以下一行:
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\] $ ”
步骤3:在根(home)目录下运行。bashrc:
- = ytet -伊甸园字幕组= -翻译
第四步:重启并打开终端,检查cmd。导航到GIt回购目录路径,就完成了。:)
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\] $"
要反映最新的更改,请运行以下命令
source ~/.bashrc
输出:
chandrakant@NDL41104 ~/Chandrakant/CodeBase/LaravelApp (development) $
root:~/project# -> root:~/project(dev)#
将以下代码添加到~/.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
首先,在主目录中打开Bash概要文件。使用默认编辑器打开和编辑bash_profile的最简单方法。
例如,我使用VS Code使用以下命令打开它:Code .bash_profile。
然后只需将以下代码粘贴到您的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\] $ "
这个函数
parse_git_branch ()
将获取分支名称,然后通过PS1,你可以在你的终端显示它。
在这里,
\u =用户名 @ =静态文本 \h =计算机名 \w =当前目录 $ =静态文本
您可以更改或删除这些变量以进行更多的自定义。
如果您在终端中第一次使用Git,或者在配置后立即使用,有时可能看不到分支名称。
如果你遇到这个问题,不要担心。在这种情况下,只需创建一个示例存储库,并在进行一些更改后提交。当commit命令执行一次时,终端将从中找到git分支。
我想要一个干净的解决方案,附加到现有的提示,而不是取代它。与其他解决方案一样,将此添加到.bashrc的底部
# function
parse_git_branch() {
if [ -n "$(git rev-parse --git-dir 2> /dev/null)" ]; then
echo ">> $(git rev-parse --abbrev-ref HEAD) >>"
fi
}
# environment customization
export PS1="\$(parse_git_branch)\n$PS1"
此设置产生如下提示符
>> branchname >>
user@host:~/current/path$
此外,我喜欢给提示符添加一些颜色,这样它就会更好地突出
export PS1="\e[0;36m\$(parse_git_branch)\e[0m\n$PS1"
是什么导致分支名称出现在CYAN中
查看这个回购:https://github.com/magicmonty/bash-git-prompt 这个提示符是“zsh的信息性git提示符”的一个移植。
通过Git克隆 将此存储库克隆到您的主目录。
git clone https://github.com/magicmonty/bash-git-prompt.git ~/.bash-git-prompt --depth=1
添加到~/.bashrc:
if [ -f "$HOME/.bash-git-prompt/gitprompt.sh" ]; then
GIT_PROMPT_ONLY_IN_REPO=1
source $HOME/.bash-git-prompt/gitprompt.sh
fi
推荐文章
- 在Bash命令提示符上添加git分支
- 匹配前后的Grep字符?
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别
- GitHub -致命:无法读取用户名https://github.com':没有这样的文件或目录
- 带有多个条件的Bash if语句将抛出错误
- 有效地测试Linux上的端口是否打开?
- 撤消git平分错误
- 发送字符串到stdin
- 使用.gitconfig配置diff工具
- 如何配置Mac OS X术语,使git有颜色?
- Visual Studio Code: .git文件夹/文件隐藏
- “node_modules”文件夹应该包含在git存储库中吗
- Shell脚本删除超过n天的目录
- 使用sudo时未找到命令