Git tracks commits over time, allowing you to follow the progression and history of your code. While you can always use Github online to view the public repository, navigating your local repo requires the use of CLI tools to view the Git commit history, like
The Non-CLI Solution: Just Use a Git Client
While you should definitely learn to use Git from the command line, as it helps to understand everything you’re doing, this is one of the few times where it really does just make more sense to have a proper interface for viewing Git history, especially when you take into account multiple branches, remotes, tags, and contributors. The experience of using online services like GitHub is clearly beneficial, so why not have it on the desktop?
However, it’s still useful to learn the commands. You might not want to use a GUI, or you may be in a remote environment over SSH, or you may just want a quick peek while you’re already at your terminal. Luckily, using
git log is fairly easy.
Using git log
git log shows a lot of info about each commit—the ref ID, the author, the date, the commit message, and if it’s the HEAD of any branches.
If you’d like to know what files are affected, you’ll need to run it with
--stat, which will display a list of files with additions and deletions.
git log --stat
If you’d like to know what actually changed in these commits, you’ll need to run it with
-p, which can be used with or without
git log --stat -p
This can be a lot to filter through, so you can sort by date:
git log --after="2014-7-1" --before="2014-7-4"
Or view by affected file:
git log -- example.json
Or with a search string:
git log -S"Hello, World!"
Or view important merge commits:
git log --merges
And, if you just want to view the changes of a single commit from the log, you can copy the hash and run
git show e9d802bdc3a61943b2c9c736194a202b4e000180
Viewing Branch History
Just having a list of commits can be messy to sort out branches. Luckily
git log provides the
--graph option which can be used alongside some
git log --graph --oneline --decorate
You can also use custom formatting if you don’t like the look of this:
--pretty=format:"%cn committed %h on %cd"
This particular set of parameters is quite useful, but there isn’t a shorthand for it, so if you use this a lot, we recommend setting an alias in
~/.bashrc, or whatever equivalent config you use for your shell:
alias gitgraph='git log --graph --oneline --decorate'