Git History and Changes
One of Git's best features is its history. You can see every change ever made to your project.
git log
Example
git logcommit 8b41d07c2e9a5f13b6d0e8a4c7f21b9d3e6a5c10 (HEAD -> main)
Author: Ayesha Khan <ayesha@example.com>
Date: Thu Oct 15 10:42:11 2026 +0500
Add basic stylesheet
commit 3f2a9c14d8e7b6a5f0c3d2e1b9a8f7e6d5c4b3a2
Author: Ayesha Khan <ayesha@example.com>
Date: Thu Oct 15 10:30:05 2026 +0500
Add home pageThe newest commit is at the top. HEAD -> main shows where you are now. Press q to leave the log if it fills the screen.
Shorter Logs
| Command | Shows |
|---|---|
git log --oneline | One line per commit |
git log -3 | Only the last 3 commits |
git log --oneline --graph --all | A drawing of all branches |
git log --author="Ali" | Commits by one person |
git log -- style.css | Commits that changed one file |
Example
git log --oneline8b41d07 (HEAD -> main) Add basic stylesheet 3f2a9c1 Add home page
git diff
git diff shows exactly which lines changed. Edit index.html, then:
Example
git diffdiff --git a/index.html b/index.html index 4c2a1b0..9e8d7f6 100644 --- a/index.html +++ b/index.html @@ -1,3 +1,3 @@ <html> -<h1>Welcome</h1> +<h1>Welcome to Ali's Bakery</h1> </html>
Lines starting with - were removed, and lines starting with + were added.
| Command | Compares |
|---|---|
git diff | Working files with the staging area (unstaged changes) |
git diff --staged | Staging area with the last commit (what you are about to commit) |
git diff 3f2a9c1 8b41d07 | Two commits |
git diff main feature | Two branches |
Run git diff --staged before every commit. It is a quick check that you are committing only what you meant to.
git show
See the full details and changes of one commit:
Example
git show 3f2a9c1You do not need to type the whole hash; the first 7 characters are enough.
Who Changed This Line?
git blame shows, for each line of a file, the last commit and author that changed it:
Example
git blame index.html3f2a9c14 (Ayesha Khan 2026-10-15 10:30:05 +0500 1) <html> a91c3e72 (Ali Raza 2026-10-15 11:05:40 +0500 2) <h1>Welcome to Ali's Bakery</h1> 3f2a9c14 (Ayesha Khan 2026-10-15 10:30:05 +0500 3) </html>
It is useful in a team when you want to ask someone why a line was written.
HEAD
HEAD is a pointer to the commit you are working on, usually the latest commit of your current branch. You can refer to older commits from it:
HEADis the current commitHEAD~1is one commit beforeHEAD~3is three commits before
Example
git diff HEAD~1 HEADThis shows what changed in the most recent commit.
Exercise
Exercise
Which command shows each commit on a single short line?
