This week's Git Tip of the Week is about grepping to find logs. You can subscribe to the feed if you want to receive new instalments automatically.
In last week's post, I talked about how to search a git repositories content with
git grep. In this second part, we'll look at how you can search the commit logs alone.
Sometimes it's useful to be able to search through the commit log to find out when a change occurred. Some projects like to embed a bug identifier in the commit message; for example, 1ac0a2 in the EGit project contains a reference to Bug: 324736.
To find this quickly (and without having to refer to an external bug tracking system to get the answer), we can use
git log --grep to find the commit message that corresponds to that particular bug.
[EGit] (master)$ git log --oneline --grep="Bug: 324736" 1ac0a29 [historyView] Reveal selected commit on filter change 20c9560 [historyView] Preserve commit selection on filter change
This is obviously useful where you have specific commit messages that include the associated bug tracker, but not so useful if you don't record that information with the commit itself.
Another use-case is when you want to find out when a particular change was introduced. For example, there might be a change in a file with the text
table.reveal(c);. Normally, you could use git blame to find out the change; but if the change was added and then subsequently removed, it might not be in the current file to change. It might also have been added elsewhere initially, then copied and pasted elsewhere.
git log has another feature which can be used to search for patterns in deltas. These effectively search the patches rather than the content of the files themselves. For example, if we wanted to search EGit for changes that introduced the above code change, then we could run:
[EGit] (master)$ git log --oneline -Gtable.reveal 1ac0a29 [historyView] Reveal selected commit on filter change 8635f79 Show commit corresponding to selection in commit graph table 390b6b1 Branches and Tags links in commit message viewer dfbdc45 Initial EGit contribution to eclipse.org
table.reveal no longer exists in the current codebase (and as such, the
git grep no longer shows this phrase), using
git log -G allows us to find when the change was introduced (and when it was removed).
git log -G in this way can be used to search history for potentially sensitive information which may have been accidentally committed into the repository. One can even set up period jobs to ensure that the history does not contain information that should not have been committed.
Come back next week for another instalment in the Git Tip of the Week series.