Git的分支击变快捷方式 [英] Git Branch Bash Variable Shortcut

查看:162
本文介绍了Git的分支击变快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建,我可以用它来指在我的Git分支电流一个bash变量:类似$分支

I am trying to create a bash variable which I can use to refer to my current branch in Git: something like $branch.

添加时间:支= $(GIT象征-REF --short -q HEAD)进入我的的.bash_profile ,我不断​​收到:致命的:不是git仓库(或任何父目录):git的当我开始一个新的终端

When adding: branch=$(git symbolic-ref --short -q HEAD) into my bash_profile, I keep getting: fatal: Not a git repository (or any of the parent directories): .git when I start a new terminal.

此外,回声$分支不打印出来的分支名称,如 git的象征-REF --short -q HEAD 会。

Furthermore, echo $branch does not print out the branch name, as git symbolic-ref --short -q HEAD would.

我希望能够使用它没有打印出来的分支名(我已经在我的提示),但做的事情,如:

I'd like to be able to use it not to print out the branch name (I already have that in my prompt) but to do things like:

混帐推起源$分支

推荐答案

你是哪个分支上取决于你在哪个目录,如果你有两个混帐工作的​​树木,〜/ A 〜/ b ,然后键入 CD〜/ A 可以让你在一个分支并键入 CD〜/ b 可以把你的另一分支。

Which branch you are on depends on which directory you are in. If you have two git work trees, ~/a and ~/b, then typing cd ~/a can put you on one branch and typing cd ~/b can put you on another branch.

所以试图设置 $分支的.bash_profile 是行不通的。您需要更新 $分支每次更改工作树木,时间可以改变当前的工作树枝的任何命令后。

So trying to set $branch in your .bash_profile isn't going to work. You need to update $branch every time you change work trees, and after any command that can change the branch of the current work tree.

做的最简单的事情是刚刚的的设置变量。相反,做一个别名:

The simplest thing to do is just not set a variable. Instead, make an alias:

alias branch='git symbolic-ref --short -q HEAD 2>/dev/null'

,然后用它是这样的:

And then use it like this:

git push origin $(branch)

或者类似这样的,如果你是老派的:

or like this if you're old-school:

git push origin `branch`

如果你真的要设置环境变量,最简单的解决方法是每次打印的提示时间,只是设置:

If you really want to set an environment variable, the simplest solution is to just set it every time you print your prompt:

_prompt_command () {
    export branch=$(git symbolic-ref --short -q HEAD 2>/dev/null)
}
export PROMPT_COMMAND=_prompt_command

请注意:你应该检查你的的.bash_profile 的.bashrc 来看看,如果你已经设置 PROMPT_COMMAND 。如果是这样,只要设置分支中的任何功能,您已经运行作为你的prompt命令。

Note: you should check your .bash_profile and .bashrc to see if you're already setting PROMPT_COMMAND. If so, just set branch in whatever function you're already running as your prompt command.

这篇关于Git的分支击变快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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