Git Log Cheatsheet
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
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
There are a couple of different ways you can figure out what changed in
- Between a Date Range
git log --since="2 weeks ago"
git whatchanged --since="2 weeks ago"
- Changes by File
See a list of commits ands changes for a particular file over its history.
git log -p filename.js
- 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.
- 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"
- 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"
- 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
- 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
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.
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.
- Tig: Text-mode interface for Git
brew install tig
- LazyGit: Simple Git Terminal UI
brew install lazygit
With the online Explain Shell tool, you can type in a shell command and it'll break apart the command and explain each section