Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

Git - finding a filename from a SHA1

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?

question from:https://stackoverflow.com/questions/460331/git-finding-a-filename-from-a-sha1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...