Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Look Inside a Commit

In this lesson, you looked inside the latest commit of your practice folder and found much more than the one line you changed.

Key Points

  • git show with no hash prints your latest commit. Its --stat option adds a count of the changes, like the one git commit prints, but that’s not everything the commit holds.
  • Every commit carries a label: the author you set up when you installed Git, the date you made the commit, and the message you typed with it.
  • Above the label sits the commit’s hash, the one you met in the last lesson, here in all 40 characters. In a small repository like this one, the first seven, like ec3c18f, are enough to point at a commit. A bigger project can need a few more to tell its commits apart, so use the short hash Git prints for you. HEAD and main in parentheses mark the commit you’re on, and that’s all you need about them for now.
  • A commit is a snapshot of every tracked file, in the version you staged. That’s where the shopping cart picture stops working. Last lesson, a clean working tree sounded like an empty cart, but checkout doesn’t empty it. The staging area still holds a copy of every tracked file, matching your last commit, so there’s nothing new to ring up. git add only swaps in a newer copy of one, and the next commit takes all of it.
  • That’s why your third commit kept the old cranberry price. Its snapshot took the copy you added before your cranberry edit.
  • Git doesn’t keep a second copy of a file that hasn’t changed. It reuses the one it already has, so snapshots stay small.
  • A commit doesn’t store a list of changed lines. When Git shows you changes, it works them out by comparing a commit’s snapshot with the one before it.
  • Every commit also records its parent, the hash of the commit right before it. git log --oneline is Git following those parents back from your latest commit. Your history is a chain that grows by one snapshot with every commit, and your first commit has no parent.
  • Git calculates the hash from the snapshot, author, date, message and parent together, so every commit gets a name of its own.

The Commands

What git show --stat printed for the latest commit:

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

00:00 Your practice folder has four commits now. The latest one returned cranberries to the regular price. So what did that commit actually save? The git show command can tell you.

00:11 With no hash, it prints your latest commit. Its --stat option adds a count of the changes, like the one git commit printed in the last lesson.

00:20 At the bottom, it counts one file changed, with one line in and one line out.

00:26 Is that what a commit is, a short list of changed lines? It’s a fair guess from this output, but a commit holds a lot more.

00:36 Start at the top, where Git prints the commit’s label. It names the author you set up when you installed Git, the date you made the commit, and the message you typed with it.

00:48 Above them sits the hash you met in the last lesson, here in full. You rarely need all forty characters. In a small repository like this one, the first seven are enough to point at a commit.

01:01 A bigger project can need a few more to tell its commits apart, so use the short hash that Git prints for you. HEAD and main in parentheses mark the commit you’re on, and that’s all you need about them for now.

01:14 Next, look at what’s inside. The command git ls-tree lists the files in a commit. Its option asks for names only, and your own short hash says which commit.

01:27 Both files are there, the notes as well as the prices. Your latest commit didn’t touch the notes, yet they’re in it. A commit is a snapshot of every tracked file, in the version you staged.

01:42 This is where the shopping cart picture stops working. Last lesson, a clean working tree sounded like an empty cart. But checkout doesn’t empty it. The staging area still holds a copy of every tracked file, matching your last commit, so there’s nothing new to ring up.

01:58 git add only swaps in a newer copy of one, and the next commit takes all of it. That’s why your third commit kept the old cranberry price. Its snapshot took the copy you added before your cranberry edit.

02:12 Git doesn’t keep a second copy of an unchanged file, though. It reuses the one it has, so snapshots stay small. So watch out for a common belief, that a commit stores only the lines you changed.

02:25 Git worked out that one changed line by comparing this snapshot with the one before it.

02:32 That one before is the last part of a commit. git show doesn’t print it, but every commit records its parent, the hash of the commit that came right before it.

02:43 Run git log with the --oneline option, as you did in the last lesson. It lists your four commits, newest first. That list is Git following the parents.

02:54 It starts at your latest commit, steps back to its parent, then back again, and again. Your first commit has no parent, so that’s where your history begins.

03:05 Each new commit you make points back to the latest one. So your history grows by one snapshot with every commit.

03:14 So a commit is a snapshot of every tracked file, with its author, date, message and parent. Git calculates the hash from all of that, so every commit gets a name of its own.

03:27 That snapshot includes every tracked file, even notes that are only meant for you. Your notes file is in your last three commits now. In the next lesson, Stop Tracking a File You Ignored, you’ll keep it out of the snapshots you make from now on, while the commits you already made keep their copy.

Become a Member to join the conversation.