Git - 从SHA1中找到一个文件名 [英] Git - finding a filename from a SHA1

查看:195
本文介绍了Git - 从SHA1中找到一个文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  git add somefile.txt 


 <$> 
$ b 然后我得到了这个文件的SHA1: c $ c> git hash-object somefile.txt

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

  git show 5a5bf28dcd7944991944cc5076c7525439830122 

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



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

解决方案

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



从SHA1哈希中检索文件名不是一种常见的操作,所以也许你可以扩展真实世界的用例呢?



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

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

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

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


I added a file to the index with:

git add somefile.txt

I then got the SHA1 for this file with:

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.

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?

解决方案

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.

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?

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

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"

这篇关于Git - 从SHA1中找到一个文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆