GitViewing History (git log)

Viewing History (git log)

git log is your window into a project’s past. By default it shows every commit reachable from the current branch — most recent first — with author, date, and message. The defaults are verbose; the real power is in the dozens of flags that let you ask very specific questions about history.

The simplest log

Bash
git log

Default output

Text
commit 1f9ab2c0e8b4a7d2f0c... (HEAD -> main, origin/main)
Author: Your Name <you@example.com>
Date:   Mon Jan  1 10:00:00 2025 +0000

    Add login form validation

commit c204c1d8e9...
Author: Your Name <you@example.com>
Date:   Mon Jan  1 09:00:00 2025 +0000

    Initial commit

Press space to scroll, q to exit. The (HEAD -> main, origin/main) bit shows the refs pointing at that commit.

The two essentials

The 90% form

Bash
git log --oneline               # one line per commit
git log --oneline --graph --all # full graph of all branches

--oneline --graph --all

Text
* 1f9ab2c (HEAD -> main, origin/main) Add login form validation
*   d4b1e0c Merge branch 'feature-x' into main
|\
| * 5a2c7e3 (origin/feature-x) Add validation helper
| * 2c1f8d9 Sketch feature-x API
* | b3a9f00 Update README
|/
* c204c1d Initial commit
Restricting how many commits

Bash
git log -n 5            # last 5 commits
git log -5              # same — shorthand
git log -1              # just the latest commit
git log v1.0..HEAD      # commits since the v1.0 tag
git log main..feature   # what feature has that main doesn't
Filtering by content

Find commits that touch a file or pattern

Bash
git log -- src/app.js                       # commits touching this file
git log --follow src/app.js                 # follow renames
git log --all -- "**/*.test.js"             # all branches, by pattern

# Commits whose message matches a regex
git log --grep="bugfix"
git log --grep="login" --grep="auth" --all-match   # both terms must appear

# Commits that add or remove a particular string ("pickaxe")
git log -S"SecretAPIKey"
git log -G"function login"   # regex flavour
Filtering by author and date

Bash
git log --author="Alice"
git log --since="2 weeks ago"
git log --since="2024-01-01" --until="2024-06-30"
git log --after=yesterday
Showing what changed

Diffs and stats

Bash
git log -p                          # full diff for each commit
git log --stat                      # per-file line stats
git log --shortstat                 # summary line per commit
git log --name-only                 # which files changed in each commit
git log --name-status               # files + A/M/D/R status
Pretty formatting

Custom one-liners

Bash
git log --pretty=format:'%h %an %s'
# 1f9ab2c Alice  Add login form
# d4b1e0c Bob    Merge branch 'x'

# With colour
git log --pretty=format:'%C(yellow)%h%Creset %C(green)%an%Creset %s'

# Common placeholders:
#   %H  — full hash         %h  — short hash
#   %an — author name        %ae — author email
#   %ar — author date relative (e.g., "3 hours ago")
#   %s  — subject            %b  — body
#   %d  — ref names
A go-to alias for log

Bash
git config --global alias.lg "log --graph --pretty=format:'%C(yellow)%h%Creset %C(red)%d%Creset %s %C(green)(%cr) %C(blue)<%an>%Creset' --abbrev-commit"

# Now: git lg
Looking at one commit in detail

Bash
git show 1f9ab2c             # full commit + diff
git show 1f9ab2c --stat      # stats only
git show 1f9ab2c:src/app.js  # contents of a file AT that commit
Following a single file across renames

Bash
git log --follow -p src/old-name.js
# follows the file even through renames
“Who wrote this line?”

Bash
git blame src/app.js | head -20
# 1f9ab2c (Alice  2025-01-01 10:00:00) function greet(name) {
# c204c1d (Bob    2024-12-30 09:00:00)   if (!name) return "Hello";
# c204c1d (Bob    2024-12-30 09:00:00)   return "Hi " + name;
Graph views and merges

Bash
git log --graph --oneline --all       # repo-wide topology
git log --graph --oneline --no-merges # hide merge commits
git log --graph --simplify-by-decoration --all  # only commits at branch/tag tips
Combining filters

Real-world combos

Bash
# Everything I committed in the last 7 days
git log --author="Your Name" --since="7 days ago" --oneline

# Bug-fix commits in the v2.0 release window
git log v1.9..v2.0 --grep="fix" --oneline

# Every commit that touched a specific function
git log -L:'function login':src/auth.js
The reflog is different from the log
`git log` shows committed history. `git reflog` shows local moves of HEAD and branch pointers — including commits that are no longer reachable from any branch. The reflog is your recovery tool after a mistake; the log is the public history.
Tip
For a quick "what's new since yesterday?" check, alias git log --oneline --since=yesterday --author=$(git config user.email)— you’ll have an instant changelog of your own work.