如何在Windows的命令提示符中显示git分支? [英] How to show my git branch in my command prompt on windows?

查看:779
本文介绍了如何在Windows的命令提示符中显示git分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Windows机器设置为unix之类.

I've set up my windows machine to be unix like.

我已经安装了gitforwindows,其中包括git-bash

I've installed gitforwindows which includes git-bash

我正在尝试在命令提示符下显示我当前的git分支,但是它不起作用.

I am trying to show my current git branch in my command prompt but it isn't working.

我通过.bashrc输入了PS1=$TIME$USER$HOST$LOCATION$BRANCH

I have PS1=$TIME$USER$HOST$LOCATION$BRANCH in by .bashrc

时间,用户,主机,位置都可以正常工作,与ubuntu和osx一样

TIME, USER, HOST, LOCATION work ok, same as ubuntu and osx

但是没有显示分支,我得到了:

However branch isn't showing and I get:

bash: command substitution: line 1: syntax error near unexpected token `)'
bash: command substitution: line 1: `git branch)'

分支是:

BRANCH=' \[\033[00:33m\]$(git_branch) \[\033[00\]\n\$ '

和git_branch函数-单独使用时可以正常运行,定义为:

and the git_branch function - which works ok when used in isolation is defined as:

git_branch () { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/' }

git_branch () { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/' }

我尝试过的每种格式都失败了:(

Every format I've tried has failed :(

PS1的其余部分如下:

Rest of PS1 looks like this:

在UNIX上的git repo中,我可以看到如下所示的分支:

On UNIX when in a git repo I can see the branch like this:

推荐答案

git-prompt.sh, one of Git's bash completion scripts, provides a function __git_ps1 whose purpose is to show the current branch. It is much more robust than a simple call to git branch. Git bash should make this available out of the box.

您应该能够通过将PS1配置为调用__git_ps1来启用它,例如

You should be able to enable it by configuring your PS1 to call __git_ps1, e.g.

PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

这将包括用户(\u),主机(\h)和工作目录(\W)以及分支,因此您可以替换任何$USER$HOST$LOCATION是.我不确定您的$TIME会做什么,但是我相信您可以自己弄清楚如何将其添加到提示中.

This will include the user (\u), host (\h), and working directory (\W) as well as the branch, so you may be able to replace whatever $USER, $HOST, and $LOCATION are. I'm not sure what your $TIME does, but I trust you can figure out how to add that to the prompt yourself.

这篇关于如何在Windows的命令提示符中显示git分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆