我尝试在bash提示符上添加我目前正在工作的git分支(签出),但没有成功。(同时保持我的当前路径显示活动目录/文件完整) 我有一个。bashrc文件在我的家,但我也看到很多人提到。profile文件…
当前回答
这里是一个简单的干净的版本,我使用:link
其他回答
如果你用的是鱼壳,那就很直接了。 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选项
现在点击使用提示按钮,你就设置好了。
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
编辑:
进一步阅读:不要重新发明轮子
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) $
我尝试了一个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\] $ "
推荐文章
- 在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时未找到命令