Understanding Git History

Mohamad's interest is in Programming (Mobile, Web, Database and Machine Learning). He is studying at the Center For Artificial Intelligence Technology (CAIT), Universiti Kebangsaan Malaysia (UKM).
Git is a version control system that keeps a detailed record of every change made to your projects. Understanding how to navigate and utilize this history is essential for effective collaboration and debugging. This tutorial will guide you through the key concepts and commands to view and manage your Git history.
What is Git History?
Git history is a record of all the changes made to a project, including what changed, when it changed, and who made the change. This history is invaluable for:
Tracking Project Progress: Monitor the evolution of your project over time.
Finding Bugs: Identify when a bug was introduced by reviewing changes.
Understanding Collaboration: See contributions made by different team members.
Key Commands for Viewing History
Here are some essential commands to help you view and understand your Git history:
git log: Show full commit history.git log --oneline: Show a summary of commits.git show <commit>: Show details of a specific commit.git diff: See unstaged changes.git diff --staged: See staged changes.
Best Practices for Viewing History
Make Frequent, Meaningful Commits: This keeps your history clear and manageable.
Write Clear Commit Messages: This helps you and your team understand the context of changes.
Use
git log --onelinefor Quick Overview: This provides a concise summary of your commit history.Use
git diffBefore Committing: Review your work to ensure accuracy.
Detailed Commands and Examples
1. See Commit History (git log)
To view the complete commit history:
bash
git log
Example Output:
apache
commit 0a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t (HEAD -> main)
Author: user123
Date: Mon, April 5, 2022, at 14:20:15 +0200
Added a new line to homepage.html
Use the arrow keys to scroll, and press
qto quit.To search, type
/followed by your search term, then pressnto jump to the next match.
2. Show Commit Details (git show <commit>)
To see all details for a specific commit:
bash
git show 09f4acd
Example Output:
apache
commit 0a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t (HEAD -> main)
Author: user123
Date: Mon, April 5, 2022, at 14:20:15 +0200
Updated index.html with a new line
diff --git a/index.html b/index.html
index 1234567..89abcde 100644
--- a/index.html
+++ b/index.html
@@ ...
+New Title
3. Compare Changes (git diff)
To see unstaged changes:
bash
git diff
Example Output:
diff --git a/index.html b/index.html
index 1234567..89abcde 100644
--- a/index.html
+++ b/index.html
@@ ...
-Old Title
+New Title
4. Compare Staged Changes (git diff --staged)
To see staged changes before committing:
bash
git diff --staged
5. Compare Two Commits
To view differences between two specific commits:
bash
git diff <commit1> <commit2>
Example:
bash
git diff 1234567 89abcde
6. Show a Summary of Commits (git log --oneline)
For a quick overview of your commit history:
bash
git log --oneline
Example Output:
09f4acd Updated index.html with a new line
8e7b2c1 Add about page
1a2b3c4 Initial commit
7. Show Commits by Author
To filter commits by a specific author:
bash
git log --author="Alice"
8. Show Recent Commits
To view commits from the last two weeks:
bash
git log --since="2 weeks ago"
9. Show Files Changed Per Commit (git log --stat)
To see files changed in each commit:
bash
git log --stat
10. Show a Branch Graph (git log --graph)
To visualize your branch history:
bash
git log --graph --oneline
Troubleshooting
Can't see your changes? Ensure you have committed your work; uncommitted changes won't appear in the history.
Log is too long? Use
git log --onelineorgit log --sinceto make it more manageable.How do I quit the log view? Press
qto exit the log or diff view.
By mastering these commands and best practices, you can effectively manage and understand your project's history in Git. Happy coding!