我尝试在bash提示符上添加我目前正在工作的git分支(签出),但没有成功。(同时保持我的当前路径显示活动目录/文件完整) 我有一个。bashrc文件在我的家,但我也看到很多人提到。profile文件…
当前回答
我想要一个干净的解决方案,附加到现有的提示,而不是取代它。与其他解决方案一样,将此添加到.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中
其他回答
我尝试了一个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)] \$
请按照以下步骤操作:(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
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
查看这个回购: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
我想要一个干净的解决方案,附加到现有的提示,而不是取代它。与其他解决方案一样,将此添加到.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中
推荐文章
- 在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时未找到命令