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 systemCommandAnswers
The recording timelinegit logWhat happened, and when?
Comparing two framesgit diffWhat exactly changed?
Zooming in on one momentgit showWhat happened in this commit?

The examples use git-practice, which has five commits by now.

git log: the timeline

Terminal
git log
Output
commit 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 page

Newest 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:

Terminal
git log --oneline
Output
7f3e2a1 (HEAD -> main) Add base styles
4b5c6d7 Add about page
9a8b7c6 Add home page
dbad70d Add more notes
2c12ea6 Add notes file

Options 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:

  1. Working directory
    git diff
  2. Staging area
    git diff --staged
  3. Last commit
CommandShows
git diffChanges not staged yet
git diff --stagedChanges about to be committed
git diff HEADEvery change since the last commit

1.Read a diff

Terminal
echo "Learning git diff" >> notes.txt
git diff
diff --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

Terminal
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

Terminal
git diff 9a8b7c6 7f3e2a1

Use hashes from git log --oneline. Add --stat for just the file names.

git show: one commit in full

Terminal
git show 4b5c6d7
commit 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 page

The 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.

Terminal
git show HEAD~1
git diff HEAD~2 HEAD

git blame: who wrote each line

Terminal
git blame notes.txt
Output (simplified)
2c12ea6 (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 diff

Each 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 historygit log --oneline
See the last 3 commitsgit log -3
See which files each commit changedgit log --stat
See the code each commit changedgit log -p
See one file's historygit log --oneline -- file
Search messagesgit log --grep="word"
Filter by authorgit log --author="Nithin"
Filter by dategit log --since="2 days ago"
Draw the branchesgit log --oneline --graph --all
See unstaged changesgit diff
See what's about to be committedgit diff --staged
Compare two commitsgit diff hash1 hash2
See one commit in fullgit show hash
See a file in an old commitgit show hash:file
See who wrote each linegit blame file

Practice

  1. Run git show on an older commit from git log --oneline.
  2. Edit home.html. Compare git diff before and after staging, then git diff --staged, then commit.
  3. Count the commits in git log --oneline -- notes.txt.
  4. Run git show HEAD~2:notes.txt.
  5. 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.