git log Command: View Commit History

By 

Updated on

12 min read

git log command showing commit history in Git

Every commit in a Git repository is recorded in the project history. The git log command lets you browse that history and see who made each change, when, and why. It is one of the most frequently used Git commands and one of the most flexible.

This guide explains how to use git log to view, filter, and format commit history.

Basic Usage

Run git log with no arguments to see the full commit history of the current branch, starting from the most recent commit:

Terminal
git log
output
commit a3f1d2e4b5c6789012345678901234567890abcd
Author: Jane Smith <jane@example.com>
Date:   Tue Mar 10 14:22:31 2026 +0100

    Add user authentication module

commit 7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c
Author: John Doe <john@example.com>
Date:   Sat Mar 7 09:15:04 2026 +0100

    Fix login form validation

Each entry shows the full commit hash, author name and email, date, and commit message. Press q to exit the log view.

Compact Output with –oneline

The default output is verbose. Use --oneline to show one commit per line, with the abbreviated hash and the subject line only:

Terminal
git log --oneline
output
a3f1d2e Add user authentication module
7b8c9d0 Fix login form validation
c1d2e3f Update README with setup instructions

This is useful for a quick overview of recent work.

Limiting the Number of Commits

To show only the last N commits, pass -n followed by the number:

Terminal
git log -n 5

You can also write it without the space:

Terminal
git log -5

Showing the Last Commit

The most common form of this is git log -1, which prints only the most recent commit on the current branch:

Terminal
git log -1
output
commit a3f1d2e4b5c6789012345678901234567890abcd
Author: Jane Smith <jane@example.com>
Date:   Tue Mar 10 14:22:31 2026 +0100

    Add user authentication module

Because only one commit is printed, this is the quickest way to confirm what you just committed, or to check the state of a branch after a merge or a rebase.

Combine it with --oneline when you only need the hash and the subject:

Terminal
git log --oneline -1
output
a3f1d2e Add user authentication module

This form is handy in scripts and shell prompts. To capture just the full hash of the latest commit, use a format string:

Terminal
git log -1 --format=%H

You can point -1 at any branch or commit reference rather than the current branch. The following shows the last commit on main without switching to it:

Terminal
git log -1 main

If you want the last commit along with its full diff, git show is the shorter equivalent of git log -1 -p.

Filtering by Author

Use --author to show only commits by a specific person. The value is a regular expression matched against the author name and email. A plain name fragment works as an unanchored match:

Terminal
git log --author="Jane"

To match multiple authors, pass --author more than once:

Terminal
git log --author="Jane" --author="John"

Filtering by Date

Use --since and --until to limit commits to a date range. These options accept a variety of formats:

Terminal
git log --since="2 weeks ago"
git log --since="2026-03-01" --until="2026-03-15"

Searching Commit Messages

Use --grep to find commits whose message contains a keyword:

Terminal
git log --grep="authentication"

The search is case-sensitive by default. Add -i to make it case-insensitive:

Terminal
git log -i --grep="fix"

Note that --grep matches the whole commit message, not only the subject line, so a keyword that appears in the body is found as well.

Searching by Code Change

Sometimes you remember the code that changed but not the commit message that came with it. The -S option, known as the pickaxe, finds commits where the number of occurrences of a string changed, which in practice means the commits that added or removed it:

Terminal
git log -S "validate_token" --oneline
output
7b8c9d0 Fix login form validation
a3f1d2e Add user authentication module

Both commits touched validate_token, one introducing it and one changing how often it appears. Add -p to see the exact hunks so you can tell which is which.

When you need a pattern instead of a fixed string, use -G with a regular expression:

Terminal
git log -G "def (login|logout)" --oneline

The difference matters: -S reports only commits that changed how many times the string occurs, while -G reports every commit whose diff contains a line matching the pattern, including a line that was merely moved.

Viewing Changed Files

To see a summary of which files were changed in each commit without the full diff, use --stat:

Terminal
git log --stat
output
commit a3f1d2e4b5c6789012345678901234567890abcd
Author: Jane Smith <jane@example.com>
Date:   Tue Mar 10 14:22:31 2026 +0100

    Add user authentication module

 src/auth.py        | 84 ++++++++++++++++++++++++++++++++++++++++++
 src/models.py      | 12 ++++++
 tests/test_auth.py | 45 +++++++++++++++++++
 3 files changed, 141 insertions(+)

Git pads the file names into a column and scales the + markers to the width of your terminal, so the bars show relative size rather than one character per line.

When you only care about which files were touched, --name-only drops the line counts entirely:

Terminal
git log --name-only --oneline
output
a3f1d2e Add user authentication module
src/auth.py
src/models.py
tests/test_auth.py

Use --name-status instead when you also need to know whether each file was added, modified, or deleted:

Terminal
git log --name-status --oneline
output
a3f1d2e Add user authentication module
A	src/auth.py
M	src/models.py
A	tests/test_auth.py

The letter in the first column is the change status: A for added, M for modified, D for deleted, and R for renamed.

To see the full diff for each commit, use -p (or --patch):

Terminal
git log -p

Combine with -n to limit the output:

Terminal
git log -p -3

Viewing History of a Specific File

To see only the commits that touched a particular file, pass the file path after --:

Terminal
git log -- path/to/file.py

The -- separator tells Git that what follows is a path, not a branch name. Add -p to see the exact changes made to that file in each commit:

Terminal
git log -p -- path/to/file.py

There is a catch worth knowing about. By default Git stops at the point where the file was renamed or moved, so a file with a long history can look almost new:

Terminal
git log --oneline -- src/auth.py
output
78e9922 Move auth module into src/

Add --follow and Git keeps walking past the rename into the file’s earlier life under its old path:

Terminal
git log --oneline --follow -- src/auth.py
output
78e9922 Move auth module into src/
199ca89 Harden token expiry check
e35e503 Add refresh token support
c442051 Add user authentication module

The option works on one file at a time, so pass a single path rather than a directory or a glob.

Branch and Graph View

To visualize how branches diverge and merge, use --graph:

Terminal
git log --oneline --graph
output
* a3f1d2e Add user authentication module
*   0f2affd Merge branch 'dark-mode'
|\
| * 3e4f5a6 Add dark mode toggle
|/
* c1d2e3f Update README with setup instructions

The |\ marks the point where a branch was merged back in, and the |/ below it marks where that branch originally split off. Everything on the indented lane belongs to dark-mode.

Keep in mind that this walks the history of the current branch only, so a feature branch that has never been merged does not appear at all. Add --all to include every branch and tag in the repository:

Terminal
git log --oneline --graph --all
output
* a3f1d2e Add user authentication module
| * 9c4b7e1 WIP: search filters
|/
*   0f2affd Merge branch 'dark-mode'
|\
| * 3e4f5a6 Add dark mode toggle
|/
* c1d2e3f Update README with setup instructions

The unmerged search-filters branch now shows up as a lane that never rejoins the trunk, which makes --all the version to reach for when you are trying to work out what is still outstanding.

Comparing Two Branches

To see commits that are in one branch but not another, use the .. notation:

Terminal
git log main..feature-branch

This shows commits reachable from feature-branch that are not yet in main, which is what you want when reviewing everything a branch adds before merging it.

To compare your local main branch with origin/main, fetch from origin first, then list the commits reachable from HEAD but not from the remote-tracking branch:

Terminal
git fetch origin
git log origin/main..HEAD --oneline

Empty output means HEAD has no commits that are missing from origin/main. Reverse the two sides to see commits in origin/main that are not in your local HEAD:

Terminal
git log HEAD..origin/main --oneline

Custom Output Format

Use --pretty=format: to define exactly what each log line shows. Some useful placeholders:

  • %H - full commit hash
  • %h - abbreviated commit hash
  • %an - author name
  • %ae - author email
  • %ad - author date, shown in whatever --date format is active
  • %ar - author date, relative
  • %s - subject (first line of commit message)

For example:

Terminal
git log --pretty=format:"%h  %an  %ar  %s"
output
a3f1d2e  Jane Smith  5 days ago  Add user authentication module
7b8c9d0  John Doe    8 days ago  Fix login form validation

The %ad placeholder is controlled separately by --date, which also changes the Date: line in the default output. Use --date=short when you want plain calendar dates:

Terminal
git log --date=short --pretty=format:"%h %ad %s"
output
a3f1d2e 2026-03-10 Add user authentication module
7b8c9d0 2026-03-07 Fix login form validation

The other formats you are likely to want are --date=relative for “2 days ago” style output, --date=iso for a full timestamp with the timezone offset, and --date=format:"%Y-%m-%d %H:%M" when you need to spell out the layout yourself.

Excluding Merge Commits

To hide merge commits and see only substantive changes, use --no-merges:

Terminal
git log --no-merges --oneline

Troubleshooting

fatal: your current branch ‘main’ does not have any commits yet
The repository has been initialized but nothing has been committed, so there is no history to walk. Make a first commit and git log starts working. You will see the same message on a branch created with git checkout --orphan before its first commit.

fatal: ambiguous argument ‘main’: unknown revision or path not in the working tree
Git could not tell whether the argument is a branch or a file, usually because the name does not exist as a revision. Put -- in front of the argument when you mean a path, as in git log -- main, and check the spelling with git branch -a when you mean a branch.

git log opens and the terminal stops responding
The output is being sent to the pager, not printed and returned. Press q to quit, or use the arrow keys and Space to scroll. To print straight to the terminal and skip the pager entirely, run git --no-pager log, which is what you want in scripts and in CI output.

–author returns nothing even though the name is correct
The pattern is matched case-sensitively against the author name and email together, so --author="PANOVSKI" misses an author recorded as Panovski. Add -i for a case-insensitive match, or search on a fragment of the email instead, which is usually more reliable than the display name.

–grep finds no commits you know exist
--grep is also case-sensitive by default. Add -i, and remember that passing several --grep patterns matches commits that satisfy any one of them unless you add --all-match.

Quick Reference

For a printable quick reference, see the Git cheatsheet .

CommandDescription
git logFull commit history
git log --onelineOne line per commit
git log -1Most recent commit only
git log --oneline -1Most recent commit, hash and subject
git log -1 --format=%HFull hash of the latest commit
git log -n 10Last 10 commits
git log --author="Name"Filter by author
git log --since="2 weeks ago"Filter by date
git log --grep="keyword"Search commit messages
git log -S "string"Commits that added or removed a string
git log -G "pattern"Commits whose diff matches a regex
git log --statShow changed files per commit
git log --name-onlyList changed file names only
git log --name-statusChanged files with add/modify/delete status
git log -pShow full diff per commit
git log -- fileHistory of a specific file
git log --follow -- fileFile history across renames
git log --oneline --graphVisual branch graph
git log --oneline --graph --allGraph of all branches
git log main..featureCommits in feature not in main
git log origin/main..HEADCommits in HEAD but not in origin/main
git log --no-mergesExclude merge commits
git log --date=shortShow plain calendar dates
git log --pretty=format:"%h %an %s"Custom output format

FAQ

How do I see how many commits each author has made?
Use git shortlog -sn. It groups the history by author and prints a commit count next to each name, sorted from most to least. Add --no-merges to leave merge commits out of the totals.

How do I see who last changed each line of a file?
Use git blame path/to/file. It annotates every line with the commit hash, author, and date of the last change.

What is the difference between git log and git reflog?
git log shows the commit history of the current branch. git reflog shows every movement of HEAD, including commits that were reset, rebased, or are no longer reachable from any branch. It is the main recovery tool if you lose commits accidentally.

How do I find a commit by its message?
Use git log --grep="search term". The pattern is matched against the whole commit message, subject and body alike, so there is nothing extra to enable to search the body. Add -i for a case-insensitive match, -E when the pattern uses extended regular expressions, and --all-match when you pass several --grep patterns and want only the commits that satisfy every one of them.

How do I show a single commit in full detail?
Use git show <hash>. It prints the commit metadata and the full diff. To show just the files changed without the diff, use git show --stat <hash>.

Conclusion

git log is the primary tool for understanding a project’s history. Start with --oneline for a quick overview, use --graph --all to visualize branches, and combine --author, --since, and --grep to zero in on specific changes. For undoing commits found in the log, see the git revert guide or how to undo the last commit . If you need to move one specific change onto another branch, see git cherry-pick .

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page