New! Explore our Programming Academy and AI Tutor - learn to code from scratch, free to start. Explore Now
Programming› Git› History, Diffs and Logs

History, Diffs and Logs

💻 Git

Read history, compare versions and find the commit that introduced a change.

Lesson 4 of 9 Tutorial
0 of 9 completed

Git history is a searchable record. Knowing how to read it turns debugging from guesswork into investigation.

By the end of this lesson you can:
  • 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.html

Comparing

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-login

Blame 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" --oneline
Tip Use git log --oneline --graph --all to understand branch structure at a glance.

Practice

  1. Show the last five commits in one line each.
  2. Compare the working directory with the last commit.
  3. 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

Exercise 1. Investigate history Medium
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.
Git exercise-1.txt
Expected Output
A readable history, the last diff and the commit that last touched each line.

Lesson Nav

Lesson sections