History, Diffs and Logs
💻 GitRead history, compare versions and find the commit that introduced a change.
Git history is a searchable record. Knowing how to read it turns debugging from guesswork into investigation.
- Read git log in useful formats.
- Compare commits and branches.
- Find who changed a line and when.
Reading Logs
The default log is verbose. Use flags to make it readable.
git log --oneline
git log --oneline --graph --all
git log --stat
git log -p -- index.htmlComparing
git diff shows unstaged changes. git diff --staged shows staged changes. Comparing two commits shows what changed between them.
git diff
git diff --staged
git diff HEAD~1 HEAD
git diff main feature-loginBlame and Search
git blame shows the last commit for each line. git log -S finds the commit that added or removed a string.
git blame index.html
git log -S "validateEmail" --onelinePractice
- Show the last five commits in one line each.
- Compare the working directory with the last commit.
- Find the commit that introduced a specific function name.
Hands-on exercise
In a repository with at least three commits:
1. Print a one-line graph of all branches.
2. Show the full diff of the most recent commit.
3. Use git blame on one file and identify the commit for its first line.
Recap
Logs show what happened, diffs show what changed and blame ties each line to a commit.
Example
git log --oneline
git log --oneline --graph --all
git log --stat
git log -p -- index.html
Exercises
1. Print a one-line graph of all branches.
2. Show the full diff of the most recent commit.
3. Use git blame on one file and identify the commit for its first line.
Expected Output
A readable history, the last diff and the commit that last touched each line.
Lesson Nav
Git Course
0/9 lessons completed
- 1 Why Version Control Matters Tutorial
- 2 Your First Repository Tutorial
- 3 Staging and Committing Tutorial
- 4 History, Diffs and Logs Tutorial
- 5 Branching Tutorial
- 6 Merging and Resolving Conflicts Tutorial
- 7 Remotes and Collaboration Tutorial
- 8 Undoing Mistakes Safely Tutorial
- 9 Project: A Team Workflow Project