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?
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>
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.
精彩评论