Working in a group of developers on a large open source project like Moodle, I get times where I need to dig in and find who made a specific change to the code and most importantly… WHY? getting the developer’s name can help get in contact with him/her to figure out why was that piece of code was introduced. And if he/she is not available, I can infer from the commit the issue id and use the development system (#Moodle uses Atlassian’s Jira – the Tracker) to find out what was the reason it was introduced.
Laziness above all… I tried the GUI tools first:
- phpStorm (138.379) – When viewing a source code file, I can click on git – show history, And get all the git commit logs of that specific file. I can click ctrl-d and compare each on with the current version. But no string search for a specific change in a specific file 🙁 and no easy way to find out what commit introduced a specific line of code (as far as I know). now way to git blame 🙁 (edit: after posting this, I found out that phpStorm can, amazingly, do it: Can Git show history for selected lines?)
- gitk – Using the search tool and setting it to “Find commit: adding/removing string:” and clicking the “down arrow” while viewing the code in patch mode, I can see the list of changes, BUT I have to read through a lot of code in each commit to find the line I am looking for. not easy 🙁
- git gui blame [file] – which was nice but no searching and only got me as far as the last committer. Same as what I got off the github.com blame view for that file.
Next, the internet…
- StackOverflow – my first and closest finds were: Git blame — prior commits? and Git: discover which commits ever touched a range of lines . which did not help. Only later on, after I got my answers (from Tim Hunt) I went back and rephrased that search to find: Finding a commit that introduced a string in Git. (my trust in StackOverflow was once again restored as the ultimate developer tool)
- Google – well, to cut a long story short… I was not using the right search words (which was also true for the above StackOverflow) and I only got these nice git blame manuals and Code Archaeology With Git (tools). Which was a good read.
Luck me, help was coming from the open source Moodle developer community. Tim hunt which is a long time respected UK Open University developer in the Moodle community started throwing pearls of digested wizdom at me which helped me figure out which commit was responsible for that specific piece of code (a string) that I was looking for and when it was initially introduced into the code. Here are those pearls:
One pearl was to:
git blame last-commit-hash^ path/filename | grep "string"
Looking at the results, I found out the one that seems relevant and:
git show "commit-hash"
Another pearl was:
git log -S 'string' path/filename
So, thank you Tim hunt and I hope it’s useful for anyone else doing archeological digging into open source projects using git.