astropanic
astropanic

Reputation: 10939

How to find 'lost' code with a constant in Git

Once upon a time, one bad guy had deleted a constant from the source code managed through our Git repository.

Nobody has noticed this for a loooong time.... until now.

Yet I want to figure out in which commit this constant is gone, and who the bad guy is.

I only know the constant's name was FOOBAR.

Is there some better approach as git blame --reverse?

Upvotes: 8

Views: 2101

Answers (2)

Tim Visher
Tim Visher

Reputation: 12864

git log like git log -- path/to/file/with/constant should get you all the commits that have ever touched that file. If the file doesn't change that often and your team is in the habit of writing good commit messages then that should get you started.

Once you find the revision where it dissapeared you then have your offender.

Another option would be git bisect to search for the offending commit using a binary search pattern if the file does change a lot. Something along the lines of:

$ git bisect start
$ git bisect bad
$ git bisect good <known-good-rev>
$ fgrep -Hn "FOOBAR" file
# Ah it is good!
$ git bisect good
$ fgrep -Hn "FOOBAR" file
# Ah it is bad!
$ git bisect bad

Continue following the instructions until you've found the revision that introduced the error. Read the man page for more detailed instructions. Another good resource to read would be the relevant section of Pro Git.

Good luck.

Upvotes: 0

cdhowie
cdhowie

Reputation: 169318

This will list all commits that added or removed the string FOOBAR from any file:

git log --all -p -SFOOBAR

Upvotes: 16

Related Questions