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

时间:2017-06-10 17:52:29

标签: git

有没有办法从指定的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对象,也没有commit-id。 如果pre-blobcommit-id不公开,并且仅post-blob可用,我想检索与<pre-blob>对应的完全相同的文件。

2 个答案:

答案 0 :(得分:3)

只有承诺才有父母。在提交的父级中解决该路径上的内容:

git rev-parse 9f38e2d9e6ca81341fecf82d881cf629effb4be2^:README.md

显示其ID,

git show 9f38e2d9e6ca81341fecf82d881cf629effb4be2^:README.md

显示其内容。

答案 1 :(得分:1)

如果您知道感兴趣的blob的散列并且blob仍然存在于repo中(即它可能是一个悬空物体,但它还没有被垃圾收集),请使用

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
相关问题