In this short article, we’ll be exploring some quick git commands that can help us in digging through our repositories’ history of commits. We’ll look at

  1. git log
  2. git shortlog
  3. git show
  4. git rev-list


git log

git log --oneline

  • A shorter version of git log where it outputs the commit logs in just one line.
  • Shows only the commit hash and message.

git-log-oneline


git log --name-status

  • Shows an overview on what files were added, modified and deleted for each commit.

git-log-name-status


git log --stat

  • Shows an additional diff stat of what were added, modified and deleted for each commit.

git-log-stat


git log -p

  • Shows the actual line/file modifications for each commit.

git-log-p


git log --author=<author>

  • Limits the commits to the specified author.

git-log-author


git log --grep=<pattern>

  • Limits the commits to ones with messages that matches the specified pattern.

git-log-grep


git log -S<string>

  • Limits the commits where the first version of the specified string was introduced.

git-log-s


git log --diff-filter=A <file>

  • Limits the commits where the specified file was added.
  • A is case sensitive and means added.
  • You can replace A with M to limit the commits where the specified file was modified
  • Or replace with D for when the file was deleted.

git-log-diff-filter


git log --diff-filter=a <file>

  • Similar to the previous but with lowercase case letters.
  • Lowercase a means to exclude added.
  • Limits for commits where the file was not added e.g modified or deleted.
  • Same logic for lowercase m or d.

git-log-diff-filter-lower


git shortlog

git shortlog or git shortlog -n

  • Summarizes the output for git log.
  • Groups commits by author and title.
  • The -n sorts the result by commit author in alphabetical order.

git-shortlog


git shorlog -s

  • Shows only the commit count per author.
  • You can also add -n to sort by commit count per author.

git-shortlog-s


git show

git show

  • Shows the actual line/file modifications for the latest commit.

git-show


git show <commit>

  • Shows the actual line/file modifications for the specified commit.

git-show-commit


git show <commit> --name-status

  • Shows an overview on what files were added, modified and deleted for the specified commit.

git-show-name-status


git show <commit> --stat

  • Shows an additional diff stat of what were added, modified and deleted for the specified commit.

git-show-stat


git rev-list

git rev-list --count HEAD

  • Shows the total commit count for the repository.
  • You can also replace HEAD with a branch name or a commit hash

git-rev-list


Conclusion

We’ve looked at the four different git commands and their respective arguments to help us dig different insights about our git history. You can try these with your own repositories and also explore different combination of arguments e.g

git log -Sspring-boot-starter-actuator -p

or

git log --name-status --diff-filter=a .gitignore

Hopefully some of these examples can help you in your day to day coding.