Git: Gitのログの表示と履歴の追跡
コミット履歴には、プロジェクトに加えられたすべての変更が記録されており、これがバージョン管理の中核となる価値です。この履歴を確認することで、プロジェクトの変遷を把握し、問題の原因を特定し、開発の経緯を分析することができます。
1. ロギングの基本概念
(1) コミット履歴とは何ですか?
コミット履歴は、すべてのコミットを時系列順に記録したもので、以下の内容が含まれます:
- コミットID:各コミットを一意に識別する40文字のSHA-1ハッシュ
- 著者情報:投稿者の氏名およびメールアドレス
- 提出日時:提出物が作成された時点のタイムスタンプ
- 提出情報:この変更に関する説明
- 親コミット:直前のコミットへのポインタ
graph TB
A[Latest Submissions<br/>HEAD] --> B[SubmitC3<br/>a1b2c3d]
B --> C[SubmitC2<br/>d4e5f6g]
C --> D[SubmitC1<br/>h7i8j9k]
D --> E[Initial Submission<br/>l0m1n2o]
style A fill:#d4edda
style E fill:#fff3cd
(2) 歴史の役割
- バージョン履歴:各時点におけるプロジェクトの状況を確認する
- 不具合の追跡:バグの原因となったコミットを特定する
- 変更履歴:コードの変更履歴を確認する
- コラボレーションの進捗管理:チームメンバーが現在どのような作業に取り組んでいるかを把握する
- ロールバック基準:どのバージョンにロールバックするかを決定する
(3) Gitのlogコマンド
git log は、コミット履歴を表示するための主要なコマンドであり、出力をカスタマイズするための幅広いオプションをサポートしています。
2. 基本的なログの確認
(1) 履歴をすべて表示
▶ サンプル:コミット履歴の表示
BASH
# View Full History
git log
# Output Format:
# commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0
# Author: Zhang San <zhangsan@example.com>
# Date: Mon Jan 1 10:00:00 2026 +0800
#
# feat: Add User Login Functionality
#
# commit d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2
# Author: Li Si <lisi@example.com>
# Date: Sun Dec 31 15:30:00 2025 +0800
#
# fix: Fix the login authentication error
(2) コンパクトモード
▶ サンプル:簡潔な表示
BASH
# One commit per line
git log --oneline
# Output:
# a1b2c3d feat: Add User Login Functionality
# d4e5f6g fix: Fix the login authentication error
# h7i8j9k docs: UpdateREADME
# l0m1n2o Initial commit
# Abbreviated form
git log --oneline --decorate
# Output:
# a1b2c3d (HEAD -> main) feat: Add User Login Functionality
# d4e5f6g (origin/main) fix: Fix the login authentication error
(3) 表示される項目の数を制限する
▶ サンプル:表示されるアイテム数の制限
BASH
# Show Recent3Next Submission
git log -3
# Or
git log -n 3
# Simplified Mode: Show Recent 5
git log --oneline -5
# Output:
# a1b2c3d feat: Add User Login Functionality
# d4e5f6g fix: Fix the login authentication error
# h7i8j9k docs: UpdateREADME
# k9l0m1n style: Code Formatting
# n2o3p4q refactor: Optimize Query Logic
3. フォーマットされたログ出力
(1) 書式設定オプション
| オプション | 説明 | 目的 |
|---|---|---|
--oneline |
簡易モード | 閲覧履歴のクイック閲覧 |
--stat |
統計を表示 | ファイル変更履歴を表示 |
-p |
変更点を表示 | 特定の変更を確認 |
--graph |
グラフィカル表示 | ブランチのマージ履歴を表示 |
--decorate |
参照を表示 | ブランチおよびタグの参照を表示 |
▶ サンプル:統計情報の表示
BASH
# Display File Change Statistics
git log --stat
# Output:
# commit a1b2c3d4e5f6...
# Author: Zhang San <zhangsan@example.com>
# Date: Mon Jan 1 10:00:00 2026 +0800
#
# feat: Add User Login Functionality
#
# src/auth/login.js | 25 +++++++++++++++++++++++++
# src/auth/index.js | 3 ++-
# 2 files changed, 26 insertions(+), 2 deletions(-)
(2) すべての相違点を表示
▶ サンプル:コミットの差分の表示
BASH
# Show the full diff for each commit
git log -p
# Show the differences from the most recent commit
git log -1 -p
# Output:
# commit a1b2c3d4e5f6...
# Author: Zhang San <zhangsan@example.com>
# Date: Mon Jan 1 10:00:00 2026 +0800
#
# feat: Add User Login Functionality
#
# diff --git a/src/auth/login.js b/src/auth/login.js
# new file mode 100644
# index 0000000..abc1234
# --- /dev/null
# +++ b/src/auth/login.js
# @@ -0,0 +1,25 @@
# +function login(username, password) {
# + // Validation Logic
# +}
(3) カスタム書式
▶ サンプル:カスタム出力形式
BASH
# Use a preset format
git log --pretty=oneline
git log --pretty=short
git log --pretty=full
git log --pretty=fuller
# Custom Format
git log --pretty=format:"%h - %an, %ar : %s"
# Output:
# a1b2c3d - Zhang San, 2 hours ago : feat: Add User Login Functionality
# d4e5f6g - Li Si, 1 day ago : fix: Fix the login authentication error
# A more detailed format
git log --pretty=format:"%C(yellow)%h%C(reset) - %C(green)%an%C(reset), %C(blue)%ar%C(reset) : %s"
# Table Format
git log --pretty=format:"%h | %an | %ad | %s" --date=short
# Output:
# a1b2c3d | Zhang San | 2026-01-01 | feat: Add User Login Functionality
# d4e5f6g | Li Si | 2025-12-31 | fix: Fix the login authentication error
(4) フォーマットのプレースホルダー
| プレースホルダー | 説明 | 出力例 |
|---|---|---|
%H |
完全ハッシュ | a1b2c3d4e5f6... |
%h |
ショートハッシュ | a1b2c3d |
%an |
著者名 | 張三 |
%ae |
著者のメールアドレス | zhangsan@example.com |
%ad |
投稿者 日付 | 2026年1月1日(月)10:00:00 |
%ar |
投稿者 日付(相対) | 2時間前 |
%s |
情報を投稿 | 機能追加 |
%d |
参照名 | (HEAD → main) |
4. ログのフィルタリングとクエリ
(1) 時間で絞り込む
▶ サンプル:期間によるフィルタリング
BASH
# After the specified date
git log --since="2026-01-01"
# Before the specified date
git log --until="2026-01-31"
# Relative Time
git log --since="1 week ago"
git log --since="2 months ago"
git log --after="yesterday"
# Time Range
git log --since="2026-01-01" --until="2026-01-31"
# Combined Use
git log --oneline --since="1 week ago" --until="yesterday"
(2) 著者で絞り込む
▶ サンプル:著者フィルター
BASH
# By Author Name
git log --author="Zhang San"
# By author's email address
git log --author="zhangsan@example.com"
# Fuzzy Matching
git log --author="Zhang"
# Combined Filtration
git log --author="Zhang San" --since="1 week ago"
(3) 提出された情報による絞り込み
▶ サンプル:提出情報による検索
BASH
# Search and Submit Information
git log --grep="fix"
# Multiple conditions(or relationship)
git log --grep="fix" --grep="feat"
# Regular Expressions
git log --grep="feat\|fix"
# Ignore case
git log --grep="FIX" -i
# Combined Filtration
git log --author="Zhang San" --grep="feat"
(4) ファイルで絞り込む
▶ サンプル:ファイル履歴の照会
BASH
# View the history of a specific file
git log -- README.md
# View Multiple Files
git log -- file1.js file2.js
# View Catalog History
git log -- src/auth/
# Show File Differences
git log -p -- README.md
# Display File Change Statistics
git log --stat -- README.md
# Show only commits that modified this file
git log --oneline -- README.md
# Output:
# a1b2c3d docs: UpdateREADME
# h7i8j9k docs: Add Installation Instructions
# l0m1n2o Initial commit
(5) 提出範囲で絞り込む
▶ サンプル:範囲クエリの送信
BASH
# View the history between two commits
git log a1b2c3d..d4e5f6g
# View the history following a specific commit
git log a1b2c3d..
# View the history leading up to a specific commit
git log ..d4e5f6g
# View Branch Differences
git log main..feature
# View the differences between the two branches
git log main...feature
5. グラフィカル表示
(1) 分岐図を表示する
▶ サンプル:グラフィカルログ
BASH
# Display the branch merge diagram
git log --graph
# Common Combinations
git log --graph --oneline --all
# Output:
# * a1b2c3d Merge branch 'feature'
# |\
# | * d4e5f6g Add feature
# * | b7c8d9e Fix bug
# |/
# * c0d1e2f Initial commit
# With decorative details
git log --graph --oneline --decorate --all
# Color Output
git log --graph --oneline --all --pretty=format:"%C(red)%h%C(reset) - %C(green)%s%C(reset)"
(2) ブランチ履歴の可視化
gitGraph
commit id: "Initial commit"
commit id: "Add feature A"
branch feature
checkout feature
commit id: "Feature work"
checkout main
commit id: "Fix bug"
merge feature id: "Merge feature"
commit id: "Release v1.0"
(3) ブランチの履歴を表示する
▶ サンプル:ブランチの履歴
BASH
# View the history of all branches
git log --graph --oneline --all --decorate
# View the history of a specific branch
git log --graph --oneline feature
# View Branch Fork Points
git log --graph --oneline --simplify-by-decoration
# View the merged commit
git log --merges --oneline
# View Unmerged Commits
git log --no-merges --oneline
6. 高度なロギング手法
(1) 二分探索
git bisect を使用して、この問題を引き起こしたコミットを特定します:
▶ サンプル:二分探索問題の解答
BASH
# Start the binary search
git bisect start
# Mark the current commit as problematic
git bisect bad
# Mark a commit as normal
git bisect good v1.0.0
# GitIt will automatically switch to the middle submission
# Marked as good or bad after testing
git bisect good
# Or
git bisect bad
# After finding and submitting a bug
# a1b2c3d is the first bad commit
# End Binary Search
git bisect reset
(2) ファイルの変更履歴を表示する
▶ サンプル:ファイル履歴の分析
BASH
# View the complete change history for the file
git log --follow -p -- filename
# View the revision history for each line of the file
git blame filename
# Output:
# a1b2c3d (Zhang San 2026-01-01 10:00:00 1) function login() {
# d4e5f6g (Li Si 2025-12-31 15:30:00 2) // Validation Logic
# h7i8j9k (Zhang San 2025-12-30 09:00:00 3) }
# View the history for a specific row
git log -L 10,20:filename
# View Function History
git log -L :functionName:filename
(3) 引用履歴
▶ サンプル:引用ログの表示
BASH
# ViewHEADMovement History
git reflog
# Output:
# a1b2c3d HEAD@{0}: commit: feat: Add Feature
# d4e5f6g HEAD@{1}: checkout: moving from feature to main
# h7i8j9k HEAD@{2}: commit: Feature work
# l0m1n2o HEAD@{3}: checkout: moving from main to feature
# View the history of a specific citation
git reflog show HEAD
git reflog show main
# Restore to the previous state
git reset HEAD@{5}
❓ よくある質問
Q ファイルの変更履歴を確認するにはどうすればよいですか?
A
git log -- <file> を使用するとファイルのコミット履歴を表示でき、git log -p -- <file> を使用すると変更の詳細を表示でき、git blame <file> を使用すると各行ごとの変更を表示できます。Q 2つのコミットの違いを確認するにはどうすればよいですか?
A
git log <commit1>..<commit2> を使用すると、2つのコミット間の履歴を確認でき、git diff <commit1> <commit2> を使用すると、特定のファイルの相違点を確認できます。Q 特定のバグを引き起こしたコミットをどのように見つければよいですか?
A
git bisect を使用して二分探索を行います。問題がないことが分かっているコミットを 1 つ、問題があることが分かっているコミットを 1 つ指定すると、Git が自動的に問題を引き起こしたコミットを特定します。Q logとreflogの違いは何ですか?
A
git log は、プロジェクトの変遷を記録したコミット履歴を表示します。一方、git reflog は、コミット、リセット、チェックアウトなどのすべての操作を含むブランチの移動履歴を表示します。コミットが破棄された場合でも、そのコミットは reflog 内で確認することができます。Q 削除したファイルの履歴を確認するにはどうすればよいですか?
A
git log --all --full-history -- <file> を使用すると、削除操作を含め、削除されたファイルの完全な履歴を確認できます。📖 まとめ
- コミット履歴は、プロジェクトに対するすべての変更を記録するものであり、バージョン管理の中核をなすものです。
git logを使用すると履歴を表示できます。幅広い書式設定やフィルタリングオプションに対応しています。- 出力形式:--oneline、--stat、-p、カスタム形式
- クエリのフィルタリング:日付、作成者、投稿詳細、ファイル、投稿期間
- グラフィカル表示:--graph オプションを指定すると、ブランチのマージ履歴が表示されます
- 高度なテクニック:bisect(二分探索)、blame(コミット履歴)、reflog(リファレンスログ)
📝 練習問題
-
基本演習:
git logのさまざまなオプション(コンパクトモード、統計情報、グラフ表示など)を使用してプロジェクトの履歴を表示し、出力形式のカスタマイズを試してみてください。 -
応用演習:フィルタオプションを使用して、特定の条件に合致するコミットを検索します。例えば、過去1週間のコミットを検索したり、特定の作成者によるすべてのコミットを検索したり、コミットメッセージに「fix」が含まれるコミットを検索したりします。
-
課題:複数のコミットがあるプロジェクトにおいて、
git bisectを使用して特定の不具合を引き起こしたコミットを特定し、二分探索の威力を体感してください。