git log: One command to rule over your git history

· by Raghu Rajagopalan · Read in about 2 min · (334 words) ·

I don’t know about you but while I’m pretty comfy with git cli for most routine operations, there are a few things that I do like a GUI for - most notably going through git history. Like most of you, I have an alias in my ~/.gitconfig to show logs on one line

[alias]
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative --date-order

While this is usually enough, it’s not for cases when I need to search through history for commits touching a specific file or having some specific word/regex. In these cases, I generally fire up Git Extensions. Recently, I found myself on a remote session and instead of cloning the repo locally and using git extensions, decided to figure out git log once and for all :)

So here goes - 4 commands to rule over your git history:

  1. Search through git commit contents for 'webpack' with -G

    $ git log  -i  -G 'webpack'
    
    # -i - case insensitive
    4834b43 - Migrate to yarn (3 weeks ago) <Raghu Rajagopalan>
    b494013 - upgrade css-loader - error query string empty fix (3 weeks ago) <Raghu Rajagopalan>
    ...
  2. If you rather want to just search through the commit log messages, then use --grep

    $ git log  -i  --grep 'webpack'
    4dbffb0 - webpack: Once again a set of fixes around Sourcemaps!! (3 weeks ago) <Raghu Rajagopalan>
    be20d55 - WIP - HMR with webpack-dev-server (3 weeks ago) <Raghu Rajagopalan>
  3. So now, once you have a commit, if you’d like to see the diff, use git show <commitid> - this will show you the commit message and a colorized diff of the commit.

  4. Finally, if you want to find all commits that touched a particular path, you can use log --follow

    $ git log --follow src/WebUI/webpack.config.js
    da9e1d2 - (origin/web, web) cleanup: format with prettier (3 weeks ago) <Raghu Rajagopalan>
    4dbffb0 - webpack: Once again a set of fixes around Sourcemaps!! (3 weeks ago) <Raghu Rajagopalan>
    f5cac5e - mulitple fixes (3 weeks ago) <Raghu Rajagopalan>