{"id":330706,"date":"2021-08-26T14:55:07","date_gmt":"2021-08-26T11:55:07","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-search-through-recent-git-commit-changes-cloudsavvy-it\/"},"modified":"2021-08-26T14:55:07","modified_gmt":"2021-08-26T11:55:07","slug":"how-to-search-through-recent-git-commit-changes-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-search-through-recent-git-commit-changes-cloudsavvy-it\/","title":{"rendered":"#How to Search Through Recent Git Commit Changes \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a28d0d022bcf\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a28d0d022bcf\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-search-through-recent-git-commit-changes-cloudsavvy-it\/#Using_Git_Log\" >Using Git Log<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-search-through-recent-git-commit-changes-cloudsavvy-it\/#Using_sed_For_Smarter_Matching\" >Using sed For Smarter Matching<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-search-through-recent-git-commit-changes-cloudsavvy-it\/#Maybe_Use_Git_Blame_Instead\" >Maybe Use Git Blame Instead<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Search Through Recent Git Commit Changes \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-2266\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/10\/e713ed70-1.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/10\/e713ed70-1.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/10\/e713ed70-1.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Git logo\" width=\"700\" height=\"299\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Tracking down when and where lines of code were added to your codebase can be a headache, but Git stores all changelogs, and does have some tools for searching through commit diffs. You can use them to find lines matching a given search string.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Using_Git_Log\"><\/span>Using Git Log<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Unfortunately sites like GitHub don\u2019t offer this functionality, so you\u2019ll have to use\u00a0<code>git log<\/code>. This command has a lot of parameters, including being able to search through commit diffs, but dealing with the output is a little unwieldy.<\/p>\n<p>In any case, Git\u2019s interactive pager display is pretty clunky for many people, so we recommend either searching in the command bar with <code>\/Search<\/code>, or piping the output directly to the console with\u00a0<code>| cat<\/code>, or to a file with <code>&gt; log.txt<\/code>, where it can be searched more effectively.<\/p>\n<p>Running this kind of search on a whole repository will probably generate very large output. You likely want to see commits in a given time range, which you can do with <code>--after<\/code>\u00a0and <code>--before<\/code>, which take dates as well as relative dates like \u201c2 week\u201d and \u201c3 month.\u201d The following command ignores commits older than a month and a half, and also ignores very recent commits.<\/p>\n<pre>git log --after=\"6 week\" --before=\"1 week\"<\/pre>\n<p>If you simply want to know which commits contain a given search string, you can use <code>-S<\/code>, which requires you to put the search string right after it with no whitespace.<\/p>\n<pre>git log --after=\"6 week\" -S'Dictionary' --stat<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13981\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/08\/4a868184.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"848\" height=\"363\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>If you\u2019d like to view the files with this output, you can use the <code>-p<\/code>\u00a0flag:<\/p>\n<pre>git log --after=\"6 week\" -S'Dictionary' --stat -p | cat<\/pre>\n<p>However, this output is\u00a0<em>massive<\/em> for any large query, and isn\u2019t much different from just opening it in your IDE. To solve that, we\u2019ll need to use <code>grep<\/code>\u00a0and <code>sed<\/code>, which means the commands are unfortunately going to get complicated.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Using_sed_For_Smarter_Matching\"><\/span>Using sed For Smarter Matching<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To match and print the actual lines in the file, you\u2019ll need to use <code>grep<\/code>, piping the output to it to print the lines that it found the pattern on. This requires you to type the pattern twice:<\/p>\n<pre>git log --after=\"6 week\" -S'Dictionary' --stat -p | grep 'Dictionary'<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13984\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/08\/6a231188.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1200\" height=\"370\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>However, this has a problem\u2014it\u2019s not longer including the commit messages or IDs. To fix this, we have to break out <code>sed<\/code>:<\/p>\n<pre>SEARCH=Dictionary &amp;&amp; git log --after=\"6 week\" -S$SEARCH --stat -p | sed -n \"\/commit\/,\/diff\/p; \/$SEARCH\/p\"<\/pre>\n<p>This command sets a <code>SEARCH<\/code> variable, since typing the search term twice is cumbersome. It runs <code>git log --stat -p<\/code>, which prints the full output, but it\u2019s passed to <code>sed<\/code>\u00a0for parsing. <code>sed<\/code>\u00a0matches all lines between \u201ccommit\u201d and \u201cdiff,\u201d which grabs the <code>--stat<\/code>\u00a0header output. Then, it <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>ends the line that it matched, producing actual usable output.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Maybe_Use_Git_Blame_Instead\"><\/span>Maybe Use Git Blame Instead<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The examples above work well for searching through an entire codebase. But, if you know what file is effected, and instead just want to understand the history of it, you might want to use <code>git blame<\/code>.<\/p>\n<p>Git-blame will print out the entire file, but annotate each line with the person who last modified it. This will let you quickly track down changes, and, in many cases, put the blame on your coworkers.<\/p>\n<p>You can use the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/git-scm.com\/docs\/git-blame\"><code>git blame<\/code><\/a>\u00a0command, but GitHub has a great GUI for it, available by clicking the file in question and pressing \u201cBlame.\u201d<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13983\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/08\/3740030f.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1087\" height=\"452\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Note that you can also view the chronological history of the file from the same interface; Git blame condenses it all to a single output.\n<\/p><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/13980\/how-to-search-through-recent-git-commit-changes\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Search Through Recent Git Commit Changes \u2013 CloudSavvy IT&#8221; Tracking down when and where lines of code were added to your codebase can be a headache, but Git stores all changelogs, and does have some tools for searching through commit diffs. You can use them to find lines matching a given search string&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":330707,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/10\/e713ed70-1.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-330706","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/330706","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=330706"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/330706\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/330707"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=330706"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=330706"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=330706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}