Is there any way to search through old commits in a #git repository for something that accidentally got deleted?

@woozle git log --follow <filename>
as long as it was in your history you'll see all commits where that file's changed

@vahnj What if I didn't know which file the missing definition was in, though? Is there a way to search for, say, the last occurrence of a piece of text, in any file?

@woozle you know a line from the file?

yeah, you can git diff between commits, and even do relative commits!

try something like this:

git diff HEAD^^^^^^^^^^^^^^^^^^^^

this'll pull out a ton of commits to diff against the head pointer of whatever branch you're on. then you can use text search against the snippet you have. that will help you pull apart the file, and then you can find the commit where you deleted it from there!

@er1n @woozle you could, but then you'd be demonstrating that I don't know git!

@woozle @er1n (he says with a facetious laugh that reveals embarrassment)

@vahnj @er1n

So, it's academic at this point because I found a backup that was old enough to include the missing piece, but...

...before that point, what I had was an invocation of a class that wasn't defined anywhere, and that's all I knew.

So I'd have needed to be able to search for "classname" across all files, including deleted, starting with the most recent commit and working backwards.

Sign in to participate in the conversation
Awoo Space

Awoo.space is a Mastodon instance where members can rely on a team of moderators to help resolve conflict, and limits federation with other instances using a specific access list to minimize abuse.

While mature content is allowed here, we strongly believe in being able to choose to engage with content on your own terms, so please make sure to put mature and potentially sensitive content behind the CW feature with enough description that people know what it's about.

Before signing up, please read our community guidelines. While it's a very broad swath of topics it covers, please do your best! We believe that as long as you're putting forth genuine effort to limit harm you might cause – even if you haven't read the document – you'll be okay!