Commits
100%

Git · Lesson 4

Commits

Learn to save a staged snapshot and identify it in Git history.

A commit records the files selected in the staging area. It also records a message, an author, and how it connects to earlier commits. You can use the identifier Git assigns to find that exact point in the history.

Setting an Identity for the Practice Repository

Git records a name and email with a commit. Set example values for this repository only. These values are labels in the history, so they do not grant access to a remote service.

$ git config --local user.name "Journey Learner"
$ git config --local user.email "learner@example.com"

Use the practice repository with index.html staged. Run git status if you need to check which files will go into the commit.

What does the --local choice do here?

Saving the Snapshot

Run git commit after staging the version you want. The -m option supplies the message on the command line.

$ git commit -m "Set the homepage heading"

A commit captures the staged version of a file. If you edit the working tree again after staging, that later edit remains outside the commit until you stage it.

You staged Welcome and then typed Hi in the file without staging again. Which heading will the commit contain?

Reading a Short History

Use git log --oneline to see compact history. Each row starts with an abbreviated commit identifier, followed by its message. Your identifier will differ from the example.

$ git log --oneline
a12c345 Set the homepage heading

Use the identifier to refer to the exact commit, since separate commits can carry identical messages. The display normally starts with the newest commit in the current line of history.

Two commits have the message Set the homepage heading. What distinguishes their individual snapshots?

Lesson complete

You finished Commits

You can now save staged work and locate it in history.

  • Set a commit identity for a practice repository.

  • Save the staged snapshot with a clear message.

  • Identify a commit in the abbreviated history.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Next Lesson
Back to Git