Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding out when code changed in git

I don't think there's an easier way to do this, but I thought I'd ask to be sure.

I have some code where an important line was deleted (by me). I wanted to find out in what commit I accidentally deleted that line. The only way I could find to do it was to start git "diff'ing" on subsequently earlier commits, one by one until I found the commit where the line was changed.

Is there any easier way to find out on what commit a particular part of code was changed?

like image 509
wadesworld Avatar asked Apr 19 '11 17:04

wadesworld


People also ask

How do I see my git history?

Explore Your History with Git On GitHub.com, you can access your project history by selecting the commit button from the code tab on your project. Locally, you can use git log . The git log command enables you to display a list of all of the commits on your current branch.

Does git keep track of changes?

Git keeps track of changes to files in the working directory of a repository by their name. When you move or rename a file, Git doesn't see that a file was moved; it sees that there's a file with a new filename, and the file with the old filename was deleted (even if the contents remain the same).

How do you know when commit changes?

Looking up changes for a specific commit If you have the hash for a commit, you can use the git show command to display the changes for that single commit. The output is identical to each individual commit when using git log -p .


2 Answers

If you only modified or added a line you could of course use git blame. If you only completely removed the line then that, of course, doesn't help you.

If you knew at least some of the text on the line that was deleted then you can use the "pickaxe" option to git log to limit the commits that you are searching.

E.g.

git log -S"important phrase" -p -- <file>
like image 147
CB Bailey Avatar answered Oct 19 '22 19:10

CB Bailey


git log -p will give you a full log with diffs, that you could search or scroll through.

git bisect will give you tool support for searching. Start it one someplace far back that has the line, tell it git bisect good when the line is present, and git bisect bad when it isn't. Eventually, Git will converge to the commit where it got deleted.

like image 31
Phil Miller Avatar answered Oct 19 '22 20:10

Phil Miller