Check Your Changes With git diff

At the end of the last lesson, you split two changes into two commits and checked each one with git status before you made it. That told you which files you’d changed, but not which lines. It was the only check you had then, so you trusted that the footers held what you meant to type. Even a small edit like that can pick up a stray keystroke, and in a file full of prices, a slip on the wrong line can go straight into the store’s history.

In this lesson, you’ll ask Git to show you the exact lines you changed before you commit them. You’ll read the format Git uses to print a change, and you’ll learn which command shows the changes you haven’t staged and which one shows the changes your next commit will record. Start with a change to the deals that git status can only half describe.

Read What You Changed

git status names the files you changed, and git diff shows the lines. To see it at work, make a small change to this week’s deals. This change isn’t on the README’s to-do list, so it won’t get in the way of your capstone. The store has more cranberries left than expected, so the price of the 12 oz bag drops from $2.79 to $2.49 for the rest of the deal week.

Open src/data/deals.js in VS Code. Prices are in cents, so find the cranberries line, change its sale value from 279 to 249, and save the file. The line now looks like this:

Language: JavaScript Filename: src/data/deals.js
{ item: "Fresh cranberries, 12 oz bag", unit: "each", regular: 349, sale: 249 },

Ask Git what it sees, first with the command you already know:

Language: Shell
$ git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   src/data/deals.js

no changes added to commit (use "git add" and/or "git commit -a")

So deals.js is modified, and that’s all git status can say. Did you change one price, or did you also bump a key and turn the eggs into 3999? To find out, run git diff, which shows the changed lines themselves:

Language: Shell
$ git diff
diff --git a/src/data/deals.js b/src/data/deals.js
index 4b0c8a7..1adb03d 100644
--- a/src/data/deals.js
+++ b/src/data/deals.js
@@ -10,5 +10,5 @@ export const deals = [
   { item: "Fresh apple cider, 1 gallon", unit: "each", regular: 799, sale: 599 },
   { item: "Sweet potatoes", unit: "lb", regular: 149, sale: 119 },
   { item: "Canned pumpkin, 15 oz", unit: "each", regular: 249, sale: 199 },
-  { item: "Fresh cranberries, 12 oz bag", unit: "each", regular: 349, sale: 279 },
+  { item: "Fresh cranberries, 12 oz bag", unit: "each", regular: 349, sale: 249 },
 ];

There’s your change, and only your change. This output is called a diff, short for difference, and Git prints it in the unified diff format, which you’ll meet again in code reviews, on GitHub, and in your editor. It looks dense at first, so take it apart from the top.

The first four lines are the header. They name the file and the two versions being compared. The version marked a/ and --- is the one Git already has, and the version marked b/ and +++ is the file you saved in your editor. The index line holds short IDs for those two versions, and you can skip it for now.

The rest is a hunk, one piece of the file with changes in it. Its first line, between the @@ signs, says where the piece is. The -10,5 means that on the --- side, it starts at line 10 and is five lines long, and the +10,5 says the same for the +++ side. After the second @@, Git adds a nearby line from above the hunk to help you find your way in a longer file. In this diff, that’s export const deals = [.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.