Viewing Commit History
Viewing commit history in Git is crucial for tracking changes, understanding the evolution of your codebase, and debugging. Here’s a detailed guide on how to view commit history using Git:
1. Basic Command
git log
The git log
command shows the commit history for the current branch. By default, it displays the commits in reverse chronological order (most recent first).
Usage:
git log
Output Example:
commit 9a1b2c3d4e5f67890abcdef1234567890abcdef
Author: John Doe <johndoe@example.com>
Date: Tue Aug 13 14:37:08 2024 +0000
Fix issue with user login
commit 1234567890abcdef1234567890abcdef12345678
Author: Jane Smith <janesmith@example.com>
Date: Mon Aug 12 09:15:22 2024 +0000
Add new feature for data export
Each entry includes the commit hash, author, date, and commit message.
2. Customizing the Output
You can customize how git log
displays information using various options:
-
One line per commit:
git log --oneline
-
Graph view (shows a graphical representation of branches and merges):
git log --graph
-
Show commits with a specific keyword in the message:
git log --grep="keyword"
-
Limit the number of commits shown:
git log -n 5
-
Show commits by a specific author:
git log --author="Author Name"
3. Viewing Detailed Commit Information
To view more detailed information about a specific commit, including the changes made:
Find the commit hash using git log
and then:
git show <commit-hash>
Usage:
git show 9a1b2c3d4e5f67890abcdef1234567890abcdef
Output Example:
commit 9a1b2c3d4e5f67890abcdef1234567890abcdef
Author: John Doe <johndoe@example.com>
Date: Tue Aug 13 14:37:08 2024 +0000
Fix issue with user login
diff --git a/login.py b/login.py
index 1234567..89abcdef 100644
--- a/login.py
+++ b/login.py
@@ -1,5 +1,5 @@
def login(username, password):
- if username and password:
+ if username and password == "secure":
return True
return False
This displays the commit message, author, date, and the differences introduced in the commit.
4. Viewing History of a Specific File
To see the commit history for a specific file:
git log <file-path>
Usage:
git log path/to/your/file.txt
Output Example:
commit 9a1b2c3d4e5f67890abcdef1234567890abcdef
Author: John Doe <johndoe@example.com>
Date: Tue Aug 13 14:37:08 2024 +0000
Fix issue with user login
diff --git a/file.txt b/file.txt
This shows all commits that modified the specified file.
5. Using Git Log with Filters
You can combine various options to filter and format the log output:
-
Show commits within a specific date range:
git log --since="2024-08-01" --until="2024-08-10"
-
Show commits by a specific path and format the output:
git log --pretty=format:"%h - %an, %ar : %s" -- path/to/file
Summary
By mastering these commands and options, you can efficiently navigate and understand your Git commit history, which is essential for managing and collaborating on code projects.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.