获取两个指定提交哈希之间的提交哈希。

huangapple go评论65阅读模式
英文:

Get commit hashes between 2 specified commit hash

问题

我正在询问的与这个问题有些类似。然而,它不能解决我的问题。我想列出在起始哈希和结束哈希之间的提交哈希(包括起始和结束)。让我举个简单的例子。对于这个仓库,如果我们转到主分支的提交历史,我想要在 f3b628f8f9c71a2cdfa052025c4a1ed78ee4c45dd4bce13a443cf12da40a77c16c1e591f4f985b47 之间的提交。所以我想要打印以下提交 -

f3b628f8f9c71a2cdfa052025c4a1ed78ee4c45d
c286db365e7374fe4d08f54077abb7fba81dd296
0bb0526b70870d57cbac9fcc8c4a7346a4ce5879
a5a7f852e45c7cadc8d1524bd4d14a1e39785aa5
9a5c33b16d07d62651ea80552e8782974c96bb8a
d4bce13a443cf12da40a77c16c1e591f4f985b47

我最初做了类似于这样的事情 - git rev-list --reverse --remotes ^f3b628f8f9c71a2cdfa052025c4a1ed78ee4c45d^ HEAD -- 但它给了我错误的结果。

之后我做了这个 git rev-list --reverse --ancestry-path f3b628f8f9c71a2cdfa052025c4a1ed78ee4c45d..HEAD,它给了我除了第一个(起始)提交之外的所有正确的提交。

所以如果有人能帮我解决这个问题,我将非常感激。另外,如果可能的话,请解释你正在使用的命令以及它是如何选择这些提交的。谢谢。

英文:

What I am asking is somewhat similar to this question. However, it doesn't solve my problem. I want to list the commit hashes present between a start hash and an end hash (including the start and end). Let me give a simple example. For this repository, if we go to the commit history of the master branch, I want commits between f3b628f8f9c71a2cdfa052025c4a1ed78ee4c45d and d4bce13a443cf12da40a77c16c1e591f4f985b47. So I want the following commits to be printed -

f3b628f8f9c71a2cdfa052025c4a1ed78ee4c45d
c286db365e7374fe4d08f54077abb7fba81dd296
0bb0526b70870d57cbac9fcc8c4a7346a4ce5879
a5a7f852e45c7cadc8d1524bd4d14a1e39785aa5
9a5c33b16d07d62651ea80552e8782974c96bb8a
d4bce13a443cf12da40a77c16c1e591f4f985b47

I initially did something like this - git rev-list --reverse --remotes ^f3b628f8f9c71a2cdfa052025c4a1ed78ee4c45d^ HEAD -- but it gave me incorrect results.

After that I did this git rev-list --reverse --ancestry-path f3b628f8f9c71a2cdfa052025c4a1ed78ee4c45d..HEAD which gave me all but the first (starting) commit correctly.

So if someone can please help me on this, I will be really grateful. Also, if possible, please explain the command that you're using and how it's selecting those commits only. Thank you.

答案1

得分: 1

git rev-list --reverse --ancestry-path f3b628f^..

差不多了,但第一个哈希可以从自身访问,所以被排除。

最常见的范围规范是双点语法。这基本上要求Git解析一系列的提交,这些提交可以从一个提交可达,但不能从另一个提交可达。

要包括f3b628f,选择父提交:f3b628f^

如果在引用的末尾放置一个^(插入符号),Git会解析它为该提交的父提交。

完整的哈希和短哈希都可以正常工作:

如果你提供SHA-1哈希的前几个字符,Git足够聪明,可以确定你所指的提交。

并且HEAD是隐含的:

你也可以省略语法的一侧,让Git假定HEAD。

英文:
git rev-list --reverse --ancestry-path f3b628f^..

You almost had it with your last example, but the first hash is reachable from itself so excluded.

> The most common range specification is the double-dot syntax. This basically asks Git to resolve a range of commits that are reachable from one commit but aren’t reachable from another.

https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection

To include f3b628f, choose the parent commit: f3b628f^.

> If you place a ^ (caret) at the end of a reference, Git resolves it to mean the parent of that commit.

https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#_ancestry_references

Both the full hash and short hash work equally:

> Git is smart enough to figure out what commit you’re referring to if you provide the first few characters of the SHA-1 hash

https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#_short_sha_1

and HEAD is implied:

> You can also leave off one side of the syntax to have Git assume HEAD.

https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#_short_sha_1

答案2

得分: 1

Git 2.38版本添加了--ancestry-path的可选参数,所以现在你可以执行以下命令:

git rev-list --ancestry-path=base base^! HEAD

这个命令从baseHEAD开始,仅列出路径中包含base的提交,但不包括base的祖先。

$ git rev-list --reverse --ancestry-path=f3b6 f3b6^! d4bc
f3b628f8f9c71a2cdfa052025c4a1ed78ee4c45d
c286db365e7374fe4d08f54077abb7fba81dd296
0bb0526b70870d57cbac9fcc8c4a7346a4ce5879
a5a7f852e45c7cadc8d1524bd4d14a1e39785aa5
9a5c33b16d07d62651ea80552e8782974c96bb8a
d4bce13a443cf12da40a77c16c1e591f4f985b47
$ 

有了这个新的选项,更复杂的用法也变得更加方便。

在添加这个选项之前,--ancestry-path base^! tip会包括base,但如果它是一个合并提交,它会显示base的父提交中可从tip到达但不通过base的子提交。

英文:

Git 2.38 added an optional argument to --ancestry-path, so now you can

git rev-list --ancestry-path=base base^! HEAD

which starts from base and HEAD and lists only commits with base in the path from those two tips but includes no ancestors of base.

$ git rev-list --reverse --ancestry-path=f3b6 f3b6^! d4bc
f3b628f8f9c71a2cdfa052025c4a1ed78ee4c45d
c286db365e7374fe4d08f54077abb7fba81dd296
0bb0526b70870d57cbac9fcc8c4a7346a4ce5879
a5a7f852e45c7cadc8d1524bd4d14a1e39785aa5
9a5c33b16d07d62651ea80552e8782974c96bb8a
d4bce13a443cf12da40a77c16c1e591f4f985b47
$ 

Even-harder-to-scrute (not actually inscrutable, just ... yeah, okay, this is wordplay, sue me) usages can get handy with that.

Before this addition, --ancestry-path base^! tip would include base but if it was a merge it would display reachable-from-tip children of base's parents that did not trace ancestry through base.

huangapple
  • 本文由 发表于 2023年6月2日 08:38:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定