This guide will explore various commands to view and manage your Git commit history.
Understanding git commits
Each commit in Git represents a snapshot of your project at a particular point in time. Commits include metadata like the author, date, and a commit message, which, when following the conventional commits style, includes a prefix to indicate the purpose of the change (e.g., feat:
, fix:
, for features and bug fixes, respectively).
Basic command: git log
The git log
command is the primary tool for viewing commit history.
Viewing commit history
To view the commit history of the current branch, run:
git log
You'll see output similar to:
commit 4a5b6c7d8e9f0g1h2i3j4k5l6m7n8o9p0q1r2s3 (HEAD -> master, origin/master)Author: Jane Doe <jane@example.com>Date: Wed Sep 29 14:33:07 2021 -0400feat: add login featurecommit 3a4b5c6d7e8f9g0h1i2j3k4l5m6n7o8p9q0r1s2Author: John Smith <john@example.com>Date: Tue Sep 28 11:22:33 2021 -0400fix: correct typo in main.py
Breaking down the output:
Commit Hash: Each commit is identified by a unique hash. In the example, the first commit hash is
4a5b6c7d8e9f0g1h2i3j4k5l6m7n8o9p0q1r2s3
. This hash is generated by Git based on the content of the commit and acts as its unique identifier.Branch Information:
(HEAD -> master, origin/master)
indicates the current state of the branch pointers.HEAD
represents the latest commit you're viewing or working on in the current branch.master
is the name of the branch you're currently on, which shows thatHEAD
is pointing to the tip of themaster
branch.origin/master
indicates the position of themaster
branch on the remote namedorigin
. The presence of bothHEAD -> master
andorigin/master
on the same commit suggests that your localmaster
branch is up to date with themaster
branch on the remote repository.Author: Shows who made the commit. The first commit was made by Jane Doe with the email
jane@example.com
.Date: The date and time when the commit was made. For example, the first commit was made on
Wed Sep 29 14:33:07 2021 -0400
. The-0400
indicates the time zone offset from UTC, showing this commit was made in a time zone four hours behind UTC.Commit Message: A brief description of what the commit does. Good commit messages provide a clear understanding of the changes made. In this example, the first commit message is
feat: add login feature
, indicating a new feature was added to support login functionality. The prefixfeat:
is a conventional shorthand used in some projects to indicate a new feature.Previous Commits: The log also shows previous commits in reverse chronological order. The second commit listed is by John Smith, with the commit message
fix: correct typo in main.py
, indicating a bug fix where a typo was corrected inmain.py
.
Listing all commits on a branch
To list all commits on a specific branch:
git log branch-name
Viewing commit history of an individual file
To see the commit history of a specific file:
git log path/to/file
This will show you a list of all of the commits that made changes to that particular file. All other commits will be excluded.
Viewing commits by date
To list commits after a specific date:
git log --after="2021-09-01"
Compact view with one commit per line
For a summary view:
git log --oneline
You'll see something like:
4a5b6c7 feat: add login feature3a4b5c6 fix: correct typo in main.py
This provides a much more compact view, including only the commit hash and the message.
Advanced usage of git log
Filtering by author
To list all commits made by a specific author:
git log --author="Jane Doe"
Viewing changes made by commits
To view the actual changes (diffs) made by each commit:
git log -p
Customizing output format
For a custom format, such as showing hashes and messages only:
git log --pretty=format:"%h - %s"
For a comprehensive list of all of the formatting options available in the git log, see the docs.
Graphical representation of branch history
To visualize branch history with a graph:
git log --graph --oneline --all
Troubleshooting: git log
not showing all commits
If git log
doesn’t seem to show the commits you're looking for, ensure you’re on the correct branch or use the --all
flag to view commits across all branches.
For more detail on the git log command including all of the various output formatting options, see the official documentation.