World map

 

BM: Baca dalam Bahasa Melayu

World 1: Save Points

Spot the difference

You will learn: how to see exactly what changed, line by line.

The problem

index.html wobbles in the working folder with a changed tag.

You edited index.html last night. Now you can’t remember what you changed. One line? Ten lines?

Watch

git diff for index.html. One red line starts with a minus: TODO: write day 3. Two green lines start with a plus: Day 3 and It felt great. Terminal output: $ git diff diff --git a/index.html b/index.html index 7420d4c..3a6b935 100644 --- a/index.html +++ b/index.html @@ -1,4 +1,5 @@ <h1>My Diary</h1> <p>Day 1: I made a repo.</p> <p>Day 2: I learned the box.</p> -<p>TODO: write day 3</p> +<p>Day 3: I made my first commit.</p> +<p>It felt great!</p>

git diff compares your folder with the box. This is a diff. Green + lines are new. Red - lines are gone.

Try it

Tap every line you added.

Tap every added line.

~/diary $ git diff

0 of 2 found

Predict

You run git add index.html. Then you run git diff again. What shows?

Pick one answer.

Watch

After git add, git diff prints nothing. git diff --staged prints the same changes again. Terminal output: $ git add index.html $ git diff $ git diff --staged diff --git a/index.html b/index.html index 7420d4c..3a6b935 100644 --- a/index.html +++ b/index.html @@ -1,4 +1,5 @@ <h1>My Diary</h1> <p>Day 1: I made a repo.</p> <p>Day 2: I learned the box.</p> -<p>TODO: write day 3</p> +<p>Day 3: I made my first commit.</p> +<p>It felt great!</p>

Plain git diff is empty now. To see what is in the box, add --staged. It compares the box with your last commit.

Watch

git log --oneline shows three hashes. git diff between the two older commits shows TODO: write day 2 replaced by Day 2. Terminal output: $ git log --oneline e1ef438 (HEAD -> main) Add day 2 entry 5ba4c72 Add home page 6d3a9b1 Add README $ git diff 5ba4c72 e1ef438 diff --git a/index.html b/index.html index 21cadf7..7420d4c 100644 --- a/index.html +++ b/index.html @@ -1,3 +1,4 @@ <h1>My Diary</h1> <p>Day 1: I made a repo.</p> -<p>TODO: write day 2</p> +<p>Day 2: I learned the box.</p> +<p>TODO: write day 3</p>

You can also compare two save points. Give git diff two hashes, older first. It shows what changed between them.

Try it

Put index.html in the box. Then show the diff of the box.

Working folder
Staging area
  • Empty
Repository
  • 3 save points
  • Add day 2 entry
  • Add home page
  • Add README

You did it!

You can now spot every change before you save it.

Go deeper (optional)

--cached means the same as --staged. git diff --name-only lists only the names of changed files. Add a file name to see only that file, like git diff index.html.

git diff --name-only prints just one name: index.html. Terminal output: $ git diff --name-only index.html