Git - finding a filename from a SHA1

前端 未结 5 1768
广开言路
广开言路 2020-11-27 13:56

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         


        
5条回答
  •  渐次进展
    2020-11-27 14:37

    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 
    

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

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

提交回复
热议问题