git-noob
git-noob

Reputation: 5937

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?

Upvotes: 48

Views: 28836

Answers (6)

Tomfox
Tomfox

Reputation: 145

Expanding on @Harold's comment on another answer, this command will give you the commit where the file was introduced and the file's path on the commit:

git describe --always <blob-hash>

Tested on git version 2.30.2.

Edit: if you don't want tag/branch names but would rather have a full commit id, you can try this:

git describe --always --exclude=\* --abbrev=40 <blob-hash>

Upvotes: 4

JaMa
JaMa

Reputation: 41

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

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

Upvotes: 4

nimrodm
nimrodm

Reputation: 23839

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

#!/bin/sh

obj_hash=$1

# 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.

Upvotes: 9

CB Bailey
CB Bailey

Reputation: 792877

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"

Upvotes: 40

brandonscript
brandonscript

Reputation: 73014

Great one-liner to do this:

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

Upvotes: 38

Baishampayan Ghose
Baishampayan Ghose

Reputation: 20686

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

git ls-tree <commit-sha1>

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

Check the manual pages for more options.

Upvotes: -1

Related Questions