Git Log Cheatsheet

AvatarElijah Manor 29 days
553 words 3 min read
Cheatsheet

Introduction

Working on the command line with git can be a bit overwhelming, so I'm starting a series of git cheatsheets focusing on various topics. This post focuses on git log.

Basic Logs

The basic log command git log will list out commits that are in the current branch.

# Logs in Current Branch
git log

# Logs last n number of commits
git log -n 5

# Commits between branch1 and branch2
git log branch1..branch2

# Commits in branch1 that are not in branch2
git log branch1 ^branch2

What Changed?

There are a couple of different ways you can figure out what changed in git

  1. Between a Date Range
git log --since="2 weeks ago"
git whatchanged --since="2 weeks ago"
  1. Changes by File

See a list of commits ands changes for a particular file over its history.

git log -p filename.js
  1. Stats and Patches

The following will show statistics about commits and show the patch information as well.

git log --stat -p

Search for Commit

Sometimes you may need to quickly find a specific commit by message or by the content contained in the commit.

  1. Pipe to Grep

You could redirect the output of git log --online to grep and search for a commit message that way.

git log --oneline | grep "Change in Commit Message"
  1. Use Log Grep Flag

Or another way would be to use the --grep flag that git log supports and search for a commit message.

git log --grep="Change in Commit Message"
  1. Search Content of Commit

However, if you wanted to search for a specific commit that contained a certain string then you can use the -S flag passing the content to search for.

git log -S"Change in Source Code""

Pretty Git Logs

  1. One-line Decorated Graph

The following command will flatten out the commits to one line, adds info about branches and tags, and where branches have diverged

git log --oneline --decorate --graph

glog # oh-my-zsh alias for the above command
glog Screenshot
  1. Other oh-my-zsh Aliases

Oh-My-Zsh comes with a bunch of aliases already setup for you, many of which happen to be git aliases. The following glol alias expands to git log --graph --pretty=\'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset\' which is very similar to glog, but provides some nice color options.

glol
glol Screenshot

Terminal Git Apps

If you want something a little more rich than pure git commands then you could try one of the following terminal apps. Of course there are native Mac and Windows apps too, but these are two nice free options that are worth trying.

  1. Tig: Text-mode interface for Git
brew install tig
Tig Screenshot
  1. LazyGit: Simple Git Terminal UI
brew install lazygit
LazyGit Screenshot

Explain Shell

With the online Explain Shell tool, you can type in a shell command and it'll break apart the command and explain each section

Explain Shell Screenshot
Share article