41

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?

git-noob
  • 5,207
  • 11
  • 31
  • 32

5 Answers5

32

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"
CB Bailey
  • 648,528
  • 94
  • 608
  • 638
  • Thanks for that. I didn't know you could do that – Jonathan Jan 20 '09 at 09:01
  • 6
    If you don't know what to put in for ``, `--all` will search across all branches in the repository. – EoghanM Sep 15 '11 at 13:53
  • 1
    My version of `git ls-tree` only accepts 1 revision as argument (git v1.7.4.4). I adapted yours to `for rev in $(git rev-list --all); do git ls-tree -r $rev | grep $SHA; done | uniq` – Daniel Böhmer Nov 01 '11 at 19:06
  • @halo: I'm fairly sure everyone's `ls-tree` takes a single revision argument, that's why I used `-n1` with `xargs`. – CB Bailey Nov 01 '11 at 21:59
  • @CharlesBailey: Ah, never knew about the `-n` option. Seems to be very useful. Thanks for the hint! However the command as listed above didn't work for me. Don't know why but luckily solved my problem in the meantime. – Daniel Böhmer Nov 02 '11 at 09:14
  • No 2018's news? 9 years and no advance in the [git](https://git-scm.com/docs) official implementation? – Peter Krauss May 07 '18 at 13:57
  • `git ls-tree -r HEAD | grep ` only works if for the file with the given SHA1 hash NO newer/child commit exists. – Rotax Oct 09 '20 at 15:26
29

Great one-liner to do this:

git rev-list --objects --all | grep <blob sha1>
brandonscript
  • 57,554
  • 29
  • 142
  • 204
  • 2
    It works correctly for two scenarios: (1) all blobs are unique, or (2) you are interested only in the latest blob with matching hash. For example, it omits the second (older) blob in my repository which has the same hash. Still very useful though. Tested with git v2.20. – akhan Dec 18 '18 at 02:20
  • This is great. Would you still do this, or is `git describe ` better now? – Harold Apr 26 '19 at 03:36
8

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.

Community
  • 1
  • 1
nimrodm
  • 21,218
  • 7
  • 51
  • 54
3

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"
JaMa
  • 31
  • 1
-1

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.

Baishampayan Ghose
  • 17,740
  • 10
  • 53
  • 59