When managing projects with Git, commit history is extremely important information. It records every change made to the project from its beginning to the present, helping you track code modification trajectories, locate issues, and even roll back to a specific version. The core tool for viewing commit history is Git’s log command. Mastering the use of the log command will give you a clearer understanding of the project’s evolution.

一、基础:最简单的提交历史查看

Directly entering the git log command will display all commit records of the project. By default, git log shows each record in reverse chronological order (with the most recent commit at the top). Each record contains the following key information:
- Commit hash (e.g., a1b2c3d4e5f6): A unique string identifying the commit (usually the first few characters are sufficient for identification).
- Author information: The committer’s name and email (only visible if Git identity is configured).
- Commit date: The specific time of the commit.
- Commit message: A description of the changes made in this commit (it is recommended to write it clearly and concisely).

Sample output (simplified):

commit a1b2c3d4e5f6
Author: 小明 <xiaoming@example.com>
Date:   Fri Sep 1 10:00:00 2023 +0800

    Fixed the input field misalignment issue on the login page

commit b2c3d4e5f6a1
Author: 小红 <xiaohong@example.com>
Date:   Thu Aug 31 14:30:00 2023 +0800

    Added the homepage carousel feature

Tip: If the output is too long, press the q key to exit the view.

二、常用参数:让log更实用

Using git log directly to view full records may be cumbersome for beginners. Mastering the following frequently used parameters can significantly improve efficiency.

1. 简洁模式:--oneline (One-line display)

If you only want to quickly browse the commit history, the --oneline parameter compresses each commit into a single line, retaining only key information. This is ideal for quick navigation.
Command: git log --oneline
Sample output:

a1b2c3d Fixed the input field misalignment issue on the login page
b2c3d4e Added the homepage carousel feature
e4f5g6h Initialized project structure

2. 图形化分支:--graph (Show branch merges)

If your project has multiple branches (e.g., master branch, feature/login branch), --graph visually represents branch merge relationships, helping you understand the project structure.
Command: git log --graph --oneline
Sample output (with branch merges):

*   a1b2c3d Fixed the input field misalignment issue on the login page
|\  
| * e4f5g6h Temporarily added test data (feature/login branch)
|/  
* b2c3d4e Added the homepage carousel feature
* e5f6g7h Optimized navigation bar styling

Explanation: * represents commit nodes, / and \ represent branch merge directions, and | represents branch continuation.

3. 查看修改内容:-p or --patch (Show diff)

To see exactly what code was changed in a commit (e.g., bug-fix details), the -p parameter displays the code differences (diff) for each commit.
Command: git log -p
Sample output (partial):

commit a1b2c3d Fixed the input field misalignment issue on the login page
Author: 小明 <xiaoming@example.com>
Date:   Fri Sep 1 10:00:00 2023 +0800

diff --git a/src/login.html b/src/login.html
index 1234567..8765432 100644
--- a/src/login.html
+++ b/src/login.html
@@ -10,7 +10,7 @@
     <input type="text" id="username" placeholder="用户名">
     <input type="password" id="password" placeholder="密码">
-    <button>Login</button>
+    <button class="btn-primary">Login</button>

Tip: Press q to exit the diff view.

4. 按时间过滤:--since/--before (Limit time range)

To view commits from a specific time period (e.g., last week, last month), use --since (includes start time) or --before (includes end time).
Common time formats: "1 week ago" (1 week ago), "2023-01-01" (specific date), "1 day ago" (1 day ago), etc.

Example: View commits from the last 3 days
Command: git log --since="3 days ago" --oneline

5. 按作者过滤:--author (Specify committer)

In team collaboration, to view commits by a specific member (e.g., commits by Xiaoming), use the --author parameter.
Command: git log --author="小明" --oneline

6. 统计修改行数:--stat (Show modification stats)

To know how many files were modified and how many lines of code were added/removed, the --stat parameter displays this statistical information.
Command: git log --stat --oneline
Sample output (partial):

a1b2c3d Fixed the input field misalignment issue on the login page | 1 file changed, 1 insertion(+), 1 deletion(-)
b2c3d4e Added the homepage carousel feature | 2 files changed, 5 insertions(+), 0 deletions(-)

三、实用技巧:组合参数更高效

In practice, you often need to combine parameters to meet complex needs:
- Show branches + graph + concise mode: git log --graph --oneline --all (--all shows commits from all branches).
- View the last 5 commits + detailed changes: git log -n 5 -p (-n 5 limits to 5 records).
- Filter commits with specific keywords: git log --oneline --grep="login" (find commits containing “login” in the message).

四、总结

Git’s log command is the core tool for viewing commit history. Mastering the following key points will help you manage project changes efficiently:
- Basic command git log: Outputs complete commit records by default.
- Concise mode --oneline: Quick browsing of history.
- Graphical branch --graph: Understanding branch merge relationships.
- Time filtering --since and author filtering --author: Accurately locate target commits.
- Combine with -p and --stat: View specific changes or statistics.

By practicing these parameters, you can quickly extract key information from a chaotic commit history, making collaboration and version management smoother!

Xiaoye