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?
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.
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).
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With