Git - 从 SHA1 中查找文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/460331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 03:33:05  来源:igfitidea点击:

Git - finding a filename from a SHA1

gitsha1

提问by git-noob

I added a file to the index with:

我向索引添加了一个文件:

git add somefile.txt

I then got the SHA1 for this file with:

然后我得到了这个文件的 SHA1:

git hash-object somefile.txt

I now have a SHA1 and I would like to retrieve the filename of the object in the index using the SHA1.

我现在有一个 SHA1,我想使用 SHA1 检索索引中对象的文件名。

git show 5a5bf28dcd7944991944cc5076c7525439830122

This command returns the file contents but not the name of the file.

此命令返回文件内容但不返回文件名。

How do I get the full filename and path back from the SHA1?

如何从 SHA1 获取完整的文件名和路径?

采纳答案by CB Bailey

There's no such direct mapping in git as the name of the file is part of the tree object that contains the file, not of the blob object that is the file's contents.

在 git 中没有这样的直接映射,因为文件名是包含文件的树对象的一部分,而不是作为文件内容的 blob 对象的一部分。

It's not a usual operation to want to retrieve a file name from a SHA1 hash so perhaps you could expand on a real world use case for it?

想要从 SHA1 散列中检索文件名不是通常的操作,所以也许您可以扩展它的实际用例?

If you're looking at current files (i.e. the HEAD commit) you can try the following.

如果您正在查看当前文件(即 HEAD 提交),您可以尝试以下操作。

git ls-tree -r HEAD | grep <SHA1>

If you want to find the contents in previous commits you'll need to do something more like this.

如果你想在以前的提交中找到内容,你需要做更多这样的事情。

git rev-list <commit-list> | \
xargs -n1 -iX sh -c "git ls-tree -r X | grep <SHA1> && echo X"

回答by brandonscript

Great one-liner to do this:

伟大的单线做到这一点:

git rev-list --objects --all | grep <blob sha1>

回答by nimrodm

The following shell script is heavily based on Which commit has this blob?and the answer provided by Aristotle Pagaltzis.

下面的 shell 脚本很大程度上基于哪个提交有这个 blob?以及亚里士多德 Pagaltzis 提供的答案。

#!/bin/sh

obj_hash=

# go over all trees
git log --pretty=format:'%T %h %s' \
| while read tree commit subject ; do
     git ls-tree -r $tree | grep  "$obj_hash" \
     | while read a b hash filename ; do
        if [ "$hash" == "$obj_hash" ]; then
          f=$filename
          echo $f
          break
        fi
        if $f ; then break; fi
      done
      if $f; then break; fi
done

I'm sure someone could beautify this script but it does work. The idea is to look at all trees commited and search for your specific hash.

我相信有人可以美化这个脚本,但它确实有效。这个想法是查看所有提交的树并搜索您的特定哈希。

回答by JaMa

git rev-list <commit-list>won't include any commits which were for example removed by rebase -iand now are referenced only by reflog, so if blob is not found by command above you should check also reflog ie like this:

git rev-list <commit-list>不会包含任何提交,例如被删除rebase -i,现在仅被 reflog 引用,所以如果上面的命令没有找到 blob,你也应该检查 reflog,即像这样:

git reflog --all | \
cut -d\  -f1 | \
xargs -n1 -iX sh -c "git ls-tree -r X | grep <BLOB_SHA> && echo X"

回答by Baishampayan Ghose

Commit the file and note the sha1 hash of the commit object. After that use

提交文件并注意提交对象的 sha1 哈希。在那之后使用

git ls-tree <commit-sha1>

and you will get the names of the files with the hashes.

您将获得带有哈希值的文件名。

Check the manual pages for more options.

查看手册页以获取更多选项。