我尝试在bash提示符上添加我目前正在工作的git分支(签出),但没有成功。(同时保持我的当前路径显示活动目录/文件完整) 我有一个。bashrc文件在我的家,但我也看到很多人提到。profile文件…
当前回答
首先,在主目录中打开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分支。
其他回答
下面是我如何配置提示符来显示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
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\$ '
如果你用的是鱼壳,那就很直接了。 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选项
现在点击使用提示按钮,你就设置好了。
请按照以下步骤操作:(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
查看这个回购: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
推荐文章
- 安装tzdata非交互式
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- 在Bash中检查变量是否存在于列表中
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 查看PS命令的全部输出
- 确保一次只运行一个shell脚本实例的快速方法
- 如何在Windows命令提示符下运行.sh ?
- 无法安装gem -未能建立gem本地扩展-无法加载这样的文件——mkmf (LoadError)
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- 如何从命令行将每两行合并为一行?
- 如何复制在bash所有目录和文件递归?