gpt4 book ai didi

GIT:获取未 merge 文件的哈希

转载 作者:行者123 更新时间:2023-12-04 00:53:25 25 4
gpt4 key购买 nike

我正在编写一个有助于解决 git merge 冲突的工具。当通过 git pull(或 fetch + merge)发生 merge 冲突时,将创建一个名为 MERGE_HEAD 的特殊引用,我可以使用它来检索特定的提交消息文件:git log HEAD..MERGE_HEAD somefile

现在,如果我之前 git stash 编辑了一些东西,然后 git stash pop 给了我一个 merge 冲突,那么我没有 merge 任何来自git 的观点,所以没有 MERGE_HEAD。这就是我寻求帮助的原因:

如何将未 merge 文件的更新日志呈现给用户并让他们选择保留哪一个?

git ls-files -u 列出了文件和一些哈希值,但它们有点奇怪,我不能用任何 git 命令来使用它们?

最佳答案

当您遇到这样的冲突时,您没有可引用的提交,因为...好吧,stash-pop 不会首先创建提交。

但无论如何,当文件发生冲突时,如果您知道正确的拼写,您可以很容易地获得文件的不同版本。来自 man gitrevisions:

  :[<n>:]<path>, e.g. :0:README, :README
A colon, optionally followed by a stage number (0 to 3) and a colon,
followed by a path, names a blob object in the index at the given
path. A missing stage number (and the colon that follows it) names
a stage 0 entry.
During a merge, stage 1 is the common ancestor, stage 2 is the target
branch’s version (typically the current branch), and stage 3 is the
version from the branch which is being merged.

在stash-pop冲突的情况下,可以通过git show :1:file获取原始版本, stash 文件为git show :2:file,工作目录版本是 git show :3:file.

关于您使用 git ls-files -u 获得的哈希值,它们类似于:

100644 a2d3a1c45e24173a1531524034bca8bc29f39dc0 1   file
100644 6a70e21770bce9fbf4440755ff20f8f00534861b 2 file
100644 8749ddd3de43774220449a6033de9c052356788b 3 file

这些是该文件不同阶段的哈希值,即 blob 本身,而不是任何提交,这就是大多数 git 命令无法处理它的原因。您可以使用 git cat-file 获取它们的内容:

$ git cat-file -t a2d3a1c45e24173a1531524034bca8bc29f39dc0
blob
$ git cat-file -p a2d3a1c45e24173a1531524034bca8bc29f39dc0
<actual contents of the file>

关于GIT:获取未 merge 文件的哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64617225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com