View commit history (git log)
Viewing Commit History (git log)
Git's commit history records all changes to a project from its creation to its current state. By viewing the commit history, you can understand the evolution of the code, identify the source of issues, or revert to specific versions. The git log
command is the primary tool for viewing this history, offering various parameters to customize the output format and content.
Basic Usage
The simplest git log
command lists all commits in reverse chronological order:
git log
Example output:
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0
Author: John Doe <john@example.com>
Date: Mon Oct 2 14:30:22 2023 +0800
Fix login page styling issues
commit b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1
Author: Jane Smith <jane@example.com>
Date: Sun Oct 1 09:15:10 2023 +0800
Add user authentication middleware
Common Parameters
Limiting Output Quantity
Use the -n
parameter to limit the number of displayed commits:
git log -n 3
Single-Line Display
The --oneline
parameter compresses each commit into a single line:
git log --oneline
Output:
a1b2c3d Fix login page styling issues
b2c3d4e Add user authentication middleware
Graphical Branch Display
The --graph
parameter displays an ASCII graph representing branch and merge history:
git log --graph --oneline
Output:
* a1b2c3d Fix login page styling issues
* b2c3d4e Add user authentication middleware
| * c3d4e5f Update README (feature-branch)
|/
* d4e5f6g Initial commit
Filtering Commits
By Author
The --author
parameter filters commits by a specific author:
git log --author="John"
By Time Range
The --since
and --until
parameters specify a time range:
git log --since="2023-09-01" --until="2023-09-30"
By File Changes
View commits affecting a specific file:
git log -- path/to/file.js
Viewing Change Details
The -p
or --patch
parameter shows the specific changes introduced by each commit:
git log -p
Example output:
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0
Author: John Doe <john@example.com>
Date: Mon Oct 2 14:30:22 2023 +0800
Fix login page styling issues
diff --git a/src/components/Login.js b/src/components/Login.js
index abc1234..def5678 100644
--- a/src/components/Login.js
+++ b/src/components/Login.js
@@ -15,7 +15,7 @@ function Login() {
<form>
<div className="form-group">
- <label style={{color: 'red'}}>Username</label>
+ <label className="text-primary">Username</label>
<input type="text" />
</div>
Customizing Output Format
The --pretty
parameter allows customizing the display format of commit information:
git log --pretty=format:"%h - %an, %ar : %s"
Output:
a1b2c3d - John Doe, 2 hours ago : Fix login page styling issues
b2c3d4e - Jane Smith, 3 days ago : Add user authentication middleware
Common format placeholders:
%h
: Abbreviated commit hash%an
: Author name%ar
: Relative time (e.g., "2 hours ago")%s
: Commit message%ad
: Author date%d
: Reference names (e.g., branches, tags)
Viewing File Change Statistics
The --stat
parameter shows statistics on files and line numbers changed in each commit:
git log --stat
Example output:
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0
Author: John Doe <john@example.com>
Date: Mon Oct 2 14:30:22 2023 +0800
Fix login page styling issues
src/components/Login.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Advanced Usage
Finding Commits Containing Specific Text
The -S
parameter searches for commits that add or remove specific text:
git log -S "useState"
Viewing Differences Between Two Commits
Compare the history between two commits:
git log commit1..commit2
Viewing Branch Differences
View commits in the feature
branch but not in the main
branch:
git log main..feature
Integration with GUI Tools
Many Git GUI tools provide more intuitive ways to view history. For example, in VS Code:
- Open the Source Control view (Ctrl+Shift+G)
- Click the "..." menu in the top-right corner
- Select "View Commit History"
This displays a graphical commit history where you can click each commit for details.
Practical Examples
Suppose we have a React project and want to view the development history of the user authentication feature:
git log --grep="auth" --oneline -p src/components/Auth/
This lists all commits with messages containing "auth" that affect files in the Auth
directory and shows the specific changes.
Another example: view all commits from last week that modified API-related files:
git log --since="last week" --name-only -- src/api/
Performance Optimization
For large repositories, git log
may become slow. Here are optimization methods:
- Limit the scope:
git log -n 50
- Disable paging:
git --no-pager log
- Use a simple format:
git log --oneline
Combining with Other Commands
git log
is often used with other commands. For example, after identifying the commit that introduced a bug, you can use git show
to view details:
git show a1b2c3d
Or use git diff
to compare two commits:
git diff b2c3d4e..a1b2c3d
Using in Scripts
git log
output can be used in automation scripts. For example, to generate a changelog:
git log --pretty=format:"- %s (%h)" v1.0.0..HEAD > CHANGELOG.md
Or to process commit history in Node.js:
const { execSync } = require('child_process');
function getRecentCommits(n = 5) {
const output = execSync(`git log -n ${n} --pretty=format:"%h|%an|%s"`).toString();
return output.split('\n').map(line => {
const [hash, author, message] = line.split('|');
return { hash, author, message };
});
}
console.log(getRecentCommits());
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn
上一篇:移动文件(git mv)
下一篇:错误跟踪流程