从 blob 对象(哈希)中获取以前的原始文件



有没有办法从指定的 blob 对象(哈希(中获取以前的原始文件。换句话说,当一个文件的blob哈希从fd871b5更改为6732f18时,我想得到一个fd871b5的内容。但不幸的是,fd871b5不是公开的,所以我尝试在更改后的 blob 哈希中使用插入符号^。根据下面的输出,插入符号^不适用于 blob 对象。有什么想法吗?

$ git show -- README.md
commit 9f38e2d9e6ca81341fecf82d881cf629effb4be2
-- snip --
diff --git a/README.md b/README.md
index fd871b5..6732f18 100644
-- snip --
$ git show 6732f18^
error: object 6732f18f21f8b4b7ffe0c015803d7cd09c597337 is a blob, not a commit
error: object 6732f18f21f8b4b7ffe0c015803d7cd09c597337 is a blob, not a commit
fatal: ambiguous argument '6732f18^': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

用例详细信息:在一些 OSS 项目中,发布的补丁有一个 blob 对象,该对象不是公共的,也不是提交 ID。当pre-blobcommit-id不是公开的,只有可用的是post-blob时,我想检索一个与<pre-blob>对应的完全相同的文件。

只有提交才有父级。 解决提交父级中该路径中的内容:

git rev-parse 9f38e2d9e6ca81341fecf82d881cf629effb4be2^:README.md

要显示其 ID,

git show 9f38e2d9e6ca81341fecf82d881cf629effb4be2^:README.md

以显示其内容。

如果您知道感兴趣的 blob 的哈希值,并且该 blob 仍然存在于存储库中(即它可能是一个悬空对象,但尚未被垃圾回收(,请使用

git cat-file -p <hash>

为了解决想法,这里有一个例子:

$ mkdir test && cd test
$ git init
Initialized empty Git repository in /Users/jubobs/Desktop/test-git/.git/
$ echo foo > README
$ git add README
$ git commit -m "Write 'foo' to README"
[master (root-commit) 73ece26] Write 'foo' to README
 1 file changed, 1 insertion(+)
 create mode 100644 README
$ echo bar >> README
$ git add README
$ git commit -m "Append 'bar' to README"
[master 7ac5ae9] Append 'bar' to README
 1 file changed, 1 insertion(+)
test-git(master)$ git show -- README
commit 7ac5ae95b49c7e493119f46fb8150c437a200df3 (HEAD -> master)
Author: Jubobs <xxxxxxxx>
Date:   Sun Jun 11 01:04:22 2017 +0100
    Append 'bar' to README
diff --git a/README b/README
index 257cc56..3bd1f0e 100644
--- a/README
+++ b/README
@@ -1 +1,2 @@
 foo
+bar
$ git cat-file -p 257cc56
foo
$ git cat-file -p 3bd1f0e
foo
bar

相关内容

最新更新