首页 > 解决方案 > 为什么通过插入符号排除提交实际上并没有在命令提示符下排除提交?

问题描述

来自https://git-scm.com/docs/git-rev-parse#_specifying_ranges

提交排除

^(插入符号)符号

要从提交中排除可访问的提交,使用前缀 ^ 表示法。例如 ^r1 r2 表示可从 r2 访问的提交,但排除可从 r1 访问的提交(即 r1 及其祖先)。

我有一个测试存储库,我已经在其中尝试过这种语法。就这个问题而言,它由三个正常提交组成:

git log --pretty=oneline topic1
77ffe1ada25be4b465be9fc9b46f63981ecc8b16 (origin/topic1, topic1) second commit of topic 1
b5803f6c59cbc9ae6b4bba81b0d5ad8cfbd8f23a start of topic 1
75400b34ebc0936dd28513c686c8adb526f063e6 (origin/master) Initial Commit

让我们尝试排除除最近提交之外的所有内容:

git log --pretty=oneline ^topic1~ topic1
77ffe1ada25be4b465be9fc9b46f63981ecc8b16 (origin/topic1, topic1) second commit of topic 1
b5803f6c59cbc9ae6b4bba81b0d5ad8cfbd8f23a start of topic 1
75400b34ebc0936dd28513c686c8adb526f063e6 (origin/master) Initial Commit

它没有用。这看起来很奇怪,因为我们完美地遵循了插入符号。假设我们尝试使用提交哈希:

git log --pretty=oneline ^b5803 topic1
77ffe1ada25be4b465be9fc9b46f63981ecc8b16 (origin/topic1, topic1) second commit of topic 1
b5803f6c59cbc9ae6b4bba81b0d5ad8cfbd8f23a start of topic 1
75400b34ebc0936dd28513c686c8adb526f063e6 (origin/master) Initial Commit

还是行不通。相比之下,其他符号按预期工作:

git log --pretty=oneline topic1 --not topic1~
77ffe1ada25be4b465be9fc9b46f63981ecc8b16 (origin/topic1, topic1) second commit of topic 1

git log --pretty=oneline topic1~..topic1
77ffe1ada25be4b465be9fc9b46f63981ecc8b16 (origin/topic1, topic1) second commit of topic 1

为什么插入符号排除符号的行为方式不同?

标签: gitcmd

解决方案


你没有说你使用的是哪个平台。但让我猜一下:你在 Windows 上,你在 Windows 命令行提示符 CMD 中输入这些命令。

CMD是一个奇怪的野兽。特别是,插入符号^是某种转义字符。要将单个插入符号传递给调用的程序,您必须在命令行中键入两个插入符号:

git log --pretty=oneline ^^topic1~ topic1

我通常远离^并使用--not

git log --pretty=oneline topic1 --not topic1~

但它有自己的注意事项,最重要的是,它的影响扩展到命令行上给出的所有后续 refs,而不仅仅是下一个。这就是为什么与原始示例相比必须交换两个 ref 规范的原因。


推荐阅读