git分支名称有快捷方式吗?

时间:2012-03-03 22:31:55

标签: git

我倾向于为git设置长分支名称(例如,step110_create_search_engine_to_replace_google)。

我应该如何将它简单地称为checkout / commit语句中的step110?

5 个答案:

答案 0 :(得分:32)

如果您使用的是类Unix系统(Linux,Mac OS X,或许还有其他系统),那么contrib/complete/git-completion.bash bash自动完成规则集可让您自动完成git命令(您可以输入git checkout step110<tab>并且您的shell将自动填充分支名称。

要激活此功能:

  • 如果您有git源代码,则在contrib/complete/中有一个文件git-completion.bash。将其置于安全的地方(例如~/.git-completion),然后将以下行添加到~/.bashrc文件中:source ~/.git-completion。重新启动shell会话或运行source ~/.git-completion以使其在当前shell会话中运行。
  • 如果您拥有git源,您可以从here(github.com)获取脚本。然后按照上面相同的说明操作。

如果你有幸使用zsh代替bash,我知道oh-my-zsh有git自动完成插件(我不知道如何在没有{{{{}的情况下激活它们1}})。

来源:

答案 1 :(得分:12)

以下是我在OS X上安装它的方法......

首先检查它是否在您的本地系统上。似乎MacPorts和Homebrew为您下载它。

$ find / -name "git-completion.bash"

否则,请下载...

$ wget https://raw.github.com/git/git/master/contrib/completion/git-completion.bash -O ~/.git-completion

如果您没有wget,可以使用Homebrew轻松安装,或使用cURL

$ vim ~/.profile

...或您选择的编辑。

然后添加......

source ~/.git-completion

如果您的自动填充功能无法自动生效...

$ source ~/.profile

...然后你有Git自动完成。

答案 2 :(得分:3)

试试这个别名:

cb = "!checkoutbranch() { local branches=`git branch | grep -i $1 | tr -d '* '`; if [[ `echo \"$branches\" | wc -l | tr -d ' '` != 1 ]]; then echo \"Matched multiple branches:\"; git branch | grep --color -i $1; exit 1; fi; git checkout $branches; }; checkoutbranch"

结帐develop分支:

git cb dev

答案 3 :(得分:2)

如果你懒得甚至按 TAB

git symbolic-ref可能会有所帮助。您可以为分支创建别名。

$ # Define short name 's1'
$ git symbolic-ref refs/heads/s1 refs/heads/step110_create_search_engine_to_replace_google
$
$ # You can use short name 's1' afterwards
$ git reset --hard s1
$ git checkout -b s1-experiment s1
$
$ # Remove the short name (don't use branch -d here or the original branch gets deleted!)
$ git symbolic-ref -d refs/heads/s1

可以以相同的方式引用远程分支以保存键入remote/ TAB 。 (在这种情况下,我建议使用refs/tags/而不是refs/heads/作为前缀,以防止意外移动远程ref。

$ git symbolic-ref refs/tags/base refs/remotes/github/a-very-long-named-remote-branch
$ git rebase -i base

答案 4 :(得分:1)

我只想补充一点,这个文件通常已经附带git。您无需再次下载。您只需找到它并运行它。

在我的系统(Centos OS)上,以下步骤有效:

$ locate completion.bash
/usr/share/doc/git-1.7.4.1/contrib/completion/git-completion.bash
$ source /usr/share/doc/git-1.7.4.1/contrib/completion/git-completion.bash

Obvioiusly指出,最好将此行添加到主目录中的.bashrc文件中,这样每次打开新shell时都不需要重复它。

在我的情况下,我会将最后一个命令添加到我的.bashrc文件

source /usr/share/doc/git-1.7.4.1/contrib/completion/git-completion.bash
相关问题