如何在gitrevisions <text>中使用大括号扩展

时间:2017-06-17 20:30:48

标签: git bash

使用GNU Bash 4.3和Git 2.1.4,我在brace expansion使用gitrevisions syntax时遇到了困难。

具体来说,我想要get the Git commit IDs for some specific commit messages。这是日志:

$ git log --format=oneline 
924d9120dec9f61d0f99a5155c51bad63d24b37f Commit 10
c62b2f68ea6b8ff047dd5f1e99010ebf434709ef Commit 9
feec2d76977521018ff33fd6e109530fdb69291d Commit 8
9a25b2ea11d0aa19af33761c7c81bd10d0f17949 Commit 7
c7b02e556738e7cd2a2459e07af731e5677ab090 Commit 6
1bf5b98cf90340e714bd0bd13c7721f94749fa5f Commit 5
8a9c25eda46b3424a870584e4a8a4f90f6e5aa2c Commit 4
0ecf2464ec82a3aa74abb6f6c2127315ef4c2495 Commit 3
e18bdb2ae52f2149421965ff69d96dba46cd7c0a Commit 2
cf10d47067bef60ae787e5421e32b4541bdf52be Commit 1

我想返回提交5和10的提交ID。作为一种解决方法,我可以在for循环中使用shell变量来执行此操作:

$ for i in {5,10}; do git rev-parse :/"Commit $i"; done
1bf5b98cf90340e714bd0bd13c7721f94749fa5f
924d9120dec9f61d0f99a5155c51bad63d24b37f

但我更喜欢更简洁的解决方案。不幸的是,即使尝试了各种转义序列,大括号展开似乎也不起作用:

$ git rev-parse :/"Commit {5,10}" # No escaping used
:/Commit {5,10}
fatal: ambiguous argument ':/Commit {5,10}': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

$ git rev-parse :/"Commit \{5,10\}" # Attempt to pass braces to shell for expansion
:/Commit \{5,10\}
fatal: ambiguous argument ':/Commit \{5,10\}': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

1 个答案:

答案 0 :(得分:2)

解决方案是将支撑部分追加到gitrevisions <text>

$ git rev-parse :/'Commit '{5,10}
1bf5b98cf90340e714bd0bd13c7721f94749fa5f
924d9120dec9f61d0f99a5155c51bad63d24b37f