71

Is it possible to check the git reflog for all commits to a specific file.

I made a commit to file foo.txt and now it no longer shows in the git history via

git log foo.txt

I want to search the reflog to find all commits to this file so I can find my "lost" commit.

cpjolicoeur
  • 12,766
  • 7
  • 48
  • 59

4 Answers4

75

Came across this while searching for an answer, which is simple: git reflog -- $PATH, which will include amends and other actions that will not be visible otherwise (though beware, reflog will be pruned by gc)

Arkadiy Kukarkin
  • 2,153
  • 14
  • 24
  • 3
    Wow -- so the rev-list didn't find my file, but reflog had it! Even had rev-list with `--all` but that still didn't find it. Thanks you just saved me a few hours. – Saad Malik Jun 05 '17 at 00:07
  • 5
    to avoid confusion, it might be more proper to use the form `git reflog -- ` the double dash explicitly denotes you're passing it a file rather than a potential reference – LightBender Jan 14 '18 at 21:02
  • 3
    This should clearly be the accepted answer. Especially since the OP does mention reflog. – Arnaud P Oct 10 '18 at 12:22
  • 3
    One answer to rule them all, one answer to find them, One answer to bring them all and in the darkness cherry-pick them. @ArnaudP is right, this answer should be the accepted one, thanks so much! – Matt d' Dec 18 '18 at 17:29
  • 2
    This is definitely the most useful command, also the most recent one as well. Helped me to get the changes back which I lost :) – Harry12345 Jan 21 '19 at 10:36
  • Would like to add, that if you have no idea what that path could have been, you can do `git reflog -- **/FILENAME` to get all commits where the file showed up (helped me on git version 2.21.0.windows.1) – OliverE Feb 18 '21 at 12:00
63

Try:

git rev-list --all -- foo.txt

This will give you a list of all commits containing foo.txt.

Kyle Decot
  • 20,715
  • 39
  • 142
  • 263
rtn
  • 127,556
  • 20
  • 111
  • 121
21

I'd use:

git rev-list --all --remotes --pretty=oneline foo.txt

The --remotes option lets you use also your remotes, the --pretty=oneline makes it display also the commit message. Very useful when you're looking for a modification pushed to remote in a branch you don't know the name of.

Jouni K. Seppänen
  • 43,139
  • 5
  • 71
  • 100
wdev
  • 2,190
  • 20
  • 26
  • 3
    I had to use `git rev-list --all --remotes --pretty=oneline -- foo.txt` (notice the `--`) – hakunin May 19 '17 at 07:48
  • 1
    While this answers your own use case, `rev-list` doesn't search "the reflog", and won't find commits that are "lost", e.g. due to a reset of a branch. – Christian Davén May 24 '21 at 05:37
3

I lost a change when I was cleaning up my history with rebase. I used something like this to search the reflogs:

for r in $(git reflog -- ${file} | cut -d ' ' -f1); do git show ${r}:${file}; done
glevand
  • 31
  • 2