Git and GitHub · Part 1 · Git basics
Looking at history
Like a security camera system
Saving commits is half of Git; reading them back is the other half. Three commands do it, like the tools of a security camera system:
| Camera system | Command | Answers |
|---|---|---|
| The recording timeline | git log | What happened, and when? |
| Comparing two frames | git diff | What exactly changed? |
| Zooming in on one moment | git show | What happened in this commit? |
The examples use git-practice, which has five commits by now.
git log: the timeline
git logcommit 7f3e2a1c9b8d4e6f0a1b2c3d4e5f6a7b8c9d0e1f (HEAD -> main)
Author: Nithin <you@example.com>
Date: Mon Nov 23 15:20:11 2026 -0500
Add base styles
commit 4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b3c
Author: Nithin <you@example.com>
Date: Mon Nov 23 15:18:42 2026 -0500
Add about pageNewest first. Each entry has the hash, the author, the date and the message. HEAD -> main marks where the project is now.
Most of the time, one line per commit is enough:
git log --oneline7f3e2a1 (HEAD -> main) Add base styles
4b5c6d7 Add about page
9a8b7c6 Add home page
dbad70d Add more notes
2c12ea6 Add notes fileOptions like --stat (files changed), -- notes.txt (one file) and --grep="page" (search messages) narrow it down. They're all in the quick reference below.
git diff: what exactly changed
git diff shows changed lines by comparing two of the three areas from Lesson 4:
- Working directorygit diff
- Staging areagit diff --staged
- Last commit
| Command | Shows |
|---|---|
git diff | Changes not staged yet |
git diff --staged | Changes about to be committed |
git diff HEAD | Every change since the last commit |
1.Read a diff
echo "Learning git diff" >> notes.txt
git diffdiff --git a/notes.txt b/notes.txtindex 3b18e51..8d2a4f0 100644--- a/notes.txt+++ b/notes.txt@@ -1,3 +1,4 @@ Hello Git Learning the three areas Third line+Learning git diff---is the old version,+++the new one.@@ -1,3 +1,4 @@: 3 lines from line 1 became 4 lines from line 1.- Lines starting with
+were added,-removed, and a space means unchanged.
There's no “changed” line: an edited line shows as removed, then added.
-body { margin: 0; }+body { margin: 0; padding: 0; }2.Check before committing
git add notes.txt
git diff --staged
git commit -m "Add diff notes"After git add, plain git diff shows nothing, because the change is staged. git diff --staged shows it.
3.Compare two commits
git diff 9a8b7c6 7f3e2a1Use hashes from git log --oneline. Add --stat for just the file names.
git show: one commit in full
git show 4b5c6d7commit 4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b3cAuthor: Nithin <you@example.com>Date: Mon Nov 23 15:18:42 2026 -0500 Add about pagediff --git a/about.html b/about.htmlnew file mode 100644index 0000000..4c8a1d2--- /dev/null+++ b/about.html@@ -0,0 +1 @@+about pageThe details, the message and the changes, together. /dev/null means the file didn't exist before. git show 9a8b7c6:notes.txt prints a file as it was in an old commit, without changing anything.
Counting back with HEAD~
Instead of copying hashes, count back from the latest commit: HEAD is the latest, HEAD~1 one before it, HEAD~2 two before, and so on.
git show HEAD~1
git diff HEAD~2 HEADgit blame: who wrote each line
git blame notes.txt2c12ea6 (Nithin 2026-11-23 14:30) Hello Git
dbad70d (Nithin 2026-11-23 14:41) Learning the three areas
dbad70d (Nithin 2026-11-23 14:41) Third line
8e9f0a1 (Nithin 2026-11-23 15:40) Learning git diffEach line shows the commit that added it. It isn't about blame: git show on that hash shows the message that explains why the line exists. The GitLens extension shows the same in VS Code.
Quick reference
| To… | Run |
|---|---|
| See the history | git log --oneline |
| See the last 3 commits | git log -3 |
| See which files each commit changed | git log --stat |
| See the code each commit changed | git log -p |
| See one file's history | git log --oneline -- file |
| Search messages | git log --grep="word" |
| Filter by author | git log --author="Nithin" |
| Filter by date | git log --since="2 days ago" |
| Draw the branches | git log --oneline --graph --all |
| See unstaged changes | git diff |
| See what's about to be committed | git diff --staged |
| Compare two commits | git diff hash1 hash2 |
| See one commit in full | git show hash |
| See a file in an old commit | git show hash:file |
| See who wrote each line | git blame file |
Practice
- Run
git showon an older commit fromgit log --oneline. - Edit
home.html. Comparegit diffbefore and after staging, thengit diff --staged, then commit. - Count the commits in
git log --oneline -- notes.txt. - Run
git show HEAD~2:notes.txt. - What happened in this diff?
-const timeout = 5000;+const timeout = 10000;Show the answer
The timeout doubled, from 5 to 10 seconds. The diff shows what changed; only the commit message can say why.