In this guide, we'll show how to explore, filter, and customize your Git commit history using powerful commands like git log
and Graphite’s gt log
command.
Understanding git commits
Every Git commit is a snapshot of your project at a specific point in time. Each commit, when following the Conventional Commits style, includes critical metadata that helps you track changes, collaborate efficiently, and debug issues. Key elements include:
- Commit hash: A unique identifier generated based on the commit’s content.
- Author: The individual responsible for the changes.
- Date: Timestamp marking when the commit was made.
- Commit message: A clear description of the changes (e.g., feat:, fix:).
Basic command: git log
To display a list of commits in Git, you can use the git log
command. By default, git log
presents the commit history of the current branch in reverse chronological order, showing details such as commit hash, author, date, and commit message.
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.
Graphite and the gt log
Command
Graphite simplifies working with multiple interdependent branches by providing streamlined CLI commands to track commit ancestry, review changes, and manage your workflow efficiently. Its intuitive design is ideal for teams that rely on stacked pull requests and advanced version control strategies.
Using gt log
for advanced stack management
The gt log
command in Graphite provides a comprehensive view of your commit history, optimized for stack-based workflows:
- Standard log view: To see a full view of all commits in your stack, run:Terminalgt log
- Simplified log: For a concise display, use:Terminalgt log short
- Detailed ancestry graph: For a more detailed view of commit relationships:Terminalgt log long
- Filtering by stack: To filter and display only the current branch’s ancestors and descendants:Here's a visual example of what you'll see in the terminal when you runTerminalgt log --stack
gt log
with Graphite:
For more detailed information on Graphite’s commands, consult the Graphite documentation.
Conclusion
Mastering Git commit history commands, including git log
and Graphite’s gt log
, is essential for efficient version control and collaboration. This guide equips you with the knowledge to navigate, filter, and customize your Git history, ensuring you stay organized and productive.