如果一个简写提交ID可以引用两个不同的提交,Git会警告我吗?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (20)

如果cee157可以引用两个不同的提交ID,如

cee157eb799af829a9a0c42c0915f55cd29818d4cee1577fecf6fc5369a80bd6e926ac5f864a754b

吉特会警告我如果我输入git log cee157?(或者Git 1.8.5.2(Apple Git-48)允许我输入git log cee1)

我认为应该这样做,虽然我找不到任何权威的消息来源说它会。

提问于
用户回答回答于

它应该给你这样的东西:

$ git log cee157
error: short SHA1 cee157 is ambiguous.
error: short SHA1 cee157 is ambiguous.
fatal: ambiguous argument 'cee157': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

我只是在一个真实的Git存储库上测试了这个问题,方法是找到带有重复前缀的提交,如下所示:

git rev-list master | cut -c-4 | sort | uniq -c | sort -nr | head

中的修订列表。master,删除前4个字符,然后丢弃其余字符,计数重复数并进行数字排序。在我相对较小的~1500提交存储库中,我发现了相当多的带有4位数字前缀的修订。我选择了一个4位的前缀,因为这似乎是Git支持的最短的法律长度。

为什么关于含糊的SHA 1的错误消息出现了两次?而不管重复的SHA 1的数量:

error: short SHA1 cee157 is ambiguous.
error: short SHA1 cee157 is ambiguous.

(均为stderr。实际上,整个输出都是打开的。stderr,什么都没有stdout)

在Windows中测试:

$ git --version
git version 1.8.1.msysgit.1

我认为可以肯定地说,如果您的版本是>=1.8.1,Git威尔警告你要小心副本。

用户回答回答于

if (!quietly && (status == SHORT_NAME_AMBIGUOUS))
    return error("short SHA1 %.*s is ambiguous.", len, hex_pfx)
if (!ds->candidate_checked)
    /*
     * If this is the only candidate, there is no point
     * calling the disambiguation hint callback.
     *
     * On the other hand, if the current candidate
     * replaced an earlier candidate that did _not_ pass
     * the disambiguation hint callback, then we do have
     * more than one objects that match the short name
     * given, so we should make sure this one matches;
     * otherwise, if we discovered this one and the one
     * that we previously discarded in the reverse order,
     * we would end up showing different results in the
     * same repository!
     */
    ds->candidate_ok = (!ds->disambiguate_fn_used ||
                        ds->fn(ds->candidate, ds->cb_data));

if (!ds->candidate_ok)
    return SHORT_NAME_AMBIGUOUS;

扫码关注云+社区