2

I have file path and blob name of a file that is not in working tree. How can I find in which commits this file present?

EDIT:

I can cat file with git cat-file -p <blob> but nor log -S, nor log <path> nor bisect give me any results.

It may happen that this file in different branch or even in some fetched but not checked out branch

ephemerr
  • 1,833
  • 19
  • 22
  • 1
    If the file used to be present and it was removed at some point you can use [`git bisect`](https://git-scm.com/docs/git-bisect) to find out the commit that removed it. – axiac Feb 02 '18 at 10:09
  • This answer might be what you need (finding all commits which contain that blob): https://stackoverflow.com/a/223890/1406321 However you will need to add `--all` to the fourth line of the shell script (starting with `git log`), as otherwise only the history of HEAD is being searched. – lucash Feb 04 '18 at 12:38

4 Answers4

3

Git Log

From here: https://git-scm.com/book/en/v2/Git-Tools-Searching

git log -S "some content in that file"

Search for a filename:

git log --follow -- my_file_name.txt

Git Bisect

git bisect start HEAD <initial_commit>
git bisect run [[ -e my_file_name.txt ]]
Unapiedra
  • 15,037
  • 12
  • 64
  • 93
2

git log --diff-filter=RD

Select only files that are Renamed or Deleted.

phd
  • 82,685
  • 13
  • 120
  • 165
1

Use git log path/to/file to show each commit that modified the file, including the one that removed it.

1615903
  • 32,635
  • 12
  • 70
  • 99
1

As asked, finding which commits have a particular blob at a particular path, git doesn't have a pushbutton for it, but it's pretty easy to generate anything you want.

git log --all --reflog --format='%H:path/to/it %H' \
| git cat-file --batch-check='%(rest) %(objectname)' \
| awk '$2==myblob{print $1}' myblob=`git rev-parse mycommit:path/to/it` \
| git log --no-walk --stdin --oneline

and for just eyeballing it just the first two lines might be enough.

jthill
  • 55,082
  • 5
  • 77
  • 137
  • 1
    I couldn't understand how this do the thing, but it do. – ephemerr Oct 17 '18 at 09:12
  • Needs more explanation. Should I be substituting `mycommit` for something? I get `fatal: invalid object name 'mycommit'`. If I try to substitute `mycommit` for `HEAD` or `main`, I get errors like `fatal: path 'foobar' does not exist in 'main'`. – Illya Moskvin Jun 30 '23 at 15:43