在Git中使用长名称在分支之间切换

时间:2013-12-09 10:34:42

标签: git branch git-branch

是否有一种简单的方法可以在分支之间切换而无需键入长分支名称?我可以用散列来引用分支中的最新提交。但是如何在检出另一个分支时获取哈希值?

我想做点什么:

> git branch -a
master ab12cd...
feature/separate-sound-channels 34efgh...
> git checkout 34efgh

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

你不能简单地检查哈希,因为虽然这将得到正确的提交,但它会使你处于“分离的HEAD”状态,这基本上意味着你的工作副本与特定的分支无关,所以git命令移动分支标签将不知道影响哪个分支。

正如@John Zwinck所提到的,git-completion.bash将在您输入git命令时执行head-completion(以及其他细节),并在命令行中接下来需要头名(分支或标记)时按Tab键:

http://git-scm.com/book/en/Git-Basics-Tips-and-Tricks#Auto-Completion

mouse:~/Documents/workspace/jackson-core (master) $ git branch -a
* master
  temp
  remotes/origin/2.0
...
mouse:~/Documents/workspace/jackson-core (master) $ git tag
2.1.0b
2.1.1b
jackson-core-2.0.0
jackson-core-2.0.0-RC1
...
mouse:~/Documents/workspace/jackson-core (master) $ git checkout <tab>
2.0                      jackson-core-2.0.0-RC3   jackson-core-2.2.1
2.1                      jackson-core-2.0.1       jackson-core-2.2.2
2.1.0b                   jackson-core-2.0.2       master
2.1.1b                   jackson-core-2.0.4       origin/2.0
2.2                      jackson-core-2.0.5       origin/2.1
FETCH_HEAD               jackson-core-2.0.6       origin/2.2
HEAD                     jackson-core-2.1.2       origin/HEAD
ORIG_HEAD                jackson-core-2.1.3       origin/gh-pages
gh-pages                 jackson-core-2.1.4       origin/master
jackson-core-2.0.0       jackson-core-2.1.5       temp
jackson-core-2.0.0-RC1   jackson-core-2.2.0-rc1
jackson-core-2.0.0-RC2   jackson-core-2.2.0b
mouse:~/Documents/workspace/jackson-core (master) $ git checkout 2.<tab>
2.0      2.1      2.1.0b   2.1.1b   2.2