Git: Viewing Git Logs and Tracking History

يسجل سجل عمليات الإرسال جميع التغييرات التي أُجريت على المشروع، وهو يمثل القيمة الأساسية لنظام التحكم في الإصدارات. ومن خلال مراجعة هذا السجل، يمكنك فهم تطور المشروع، وتتبع مصدر المشكلات، وتحليل مسار التطوير.

1. المفاهيم الأساسية للتسجيل

(1) ما هو سجل عمليات الالتزام؟

سجل عمليات الالتزام هو سجل مرتب ترتيبًا زمنيًّا لجميع عمليات الالتزام، ويشمل:

100%
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 المؤلف والتاريخ الاثنين 1 يناير الساعة 10:00:00 عام 2026
%ar المؤلف التاريخ (نسبي) منذ ساعتين
%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) عرض مخطط الفروع

▶ مثال: السجل البياني

100%
# 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) عرض تاريخ الفروع

100%
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) عرض سجل الفروع

▶ مثال: سجل الفروع

100%
# 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) Citation Log

▶ مثال: عرض سجل الاستشهادات

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}


❓ أسئلة شائعة

س كيف يمكنني الاطلاع على سجل التعديلات لملف ما؟
ج استخدم git log -- <file> لعرض سجل عمليات الالتزام الخاصة بالملف، وgit log -p -- <file> لعرض التغييرات بالتفصيل، وgit blame <file> لعرض التغييرات الخاصة بكل سطر.
س كيف يمكنني الاطلاع على الاختلافات بين عمليتي التثبيت؟
ج استخدم git log <commit1>..<commit2> لعرض سجل التغييرات بين عمليتي التثبيت، واستخدم git diff <commit1> <commit2> لعرض الاختلافات في ملف معين.
س كيف يمكنني العثور على الالتزام الذي تسبب في ظهور خطأ معين؟
ج Use git bisect to perform a binary search. Mark one commit that you know is fine and one that you know has a problem, and Git will automatically pinpoint the commit that introduced the issue.
س ما الفرق بين «log» و«reflog»؟
ج git log يعرض سجل عمليات الإرسال، وهو سجل لتطور المشروع؛ أما git reflog فيعرض سجل تحركات الفروع، بما في ذلك جميع العمليات مثل عمليات الإرسال وإعادة الضبط والسحب — وحتى إذا تم تجاهل عملية إرسال ما، فلا يزال من الممكن العثور عليها في سجل المراجع (reflog).
س كيف يمكنني الاطلاع على سجل الملفات المحذوفة؟
ج استخدم git log --all --full-history -- <file> لعرض السجل الكامل للملفات المحذوفة، بما في ذلك إجراءات الحذف.

📖 ملخص


📝 تمارين

  1. تمرين أساسي: استخدم الخيارات المختلفة لـ git log لعرض سجل المشروع، بما في ذلك الوضع المضغوط والإحصائيات والعروض البيانية، وحاول تخصيص تنسيق الإخراج.

  2. Advanced Exercise: Use filter options to find commits that meet specific criteria, such as: finding commits from the past week, finding all commits by a specific author, or finding commits whose commit message contains "fix."

  3. Challenge: Use git bisect to locate the commit that introduced a specific issue in a project with multiple commits, and experience the power of binary search.

Web-Tutorial.com

فريق Web-Tutorial التقني

منصة دروس برمجية يديرها عدة مطورين. كل درس يتم كتابته ومراجعته بواسطة مطورين متخصصين في المجال. نعمل على ضمان دقة وموثوقية المحتوى — إذا لاحظت أي مشكلة، فيرجى إخبارنا.

100%