Course outline · 0% complete

0/28 lessons0%

Course overview →

git commit

lesson 2-3 · ~11 min · 5/28

Making it permanent

The commit is what every previous step exists for: it is the durable save point that undo (unit 4), branches (unit 5), and teamwork (unit 7) all operate on. Until you commit, Git is watching but nothing is actually protected.

One-time setup first. Every commit records an author, so before your first ever commit Git asks you to introduce yourself (once per computer, not per project):

$ git config --global user.name "Ada Lovelace"
$ git config --global user.email "ada@example.com"

Now the main event. git commit -m "message" takes everything in the staging area and saves it as a permanent snapshot:

$ git commit -m "Add pancake recipe"
[main (root-commit) a1b2c3d] Add pancake recipe
 1 file changed, 4 insertions(+)
 create mode 100644 pancakes.txt

Reading the output: the commit was recorded on main — the default branch git status has been reporting since lesson 2-1 — it was the very first commit (root-commit), its short hash is a1b2c3d, and it contained one file with four added lines. The -m flag attaches the message. Forget it and Git opens a text editor demanding one, every commit must have a message.

The loop you'll run forever

Daily Git is a three-beat rhythm:

  1. Edit files in the working directory.
  2. Stage the changes you want to keep: git add.
  3. Commit with a message: git commit -m "...".

Then repeat, dozens of times a day. Two habits that make future-you grateful:

  • Commit small and often. One logical change per commit ("Fix login typo", not "3 weeks of work").
  • Commit working states. Each snapshot should be a save point worth returning to.

After a commit, git status reports nothing to commit, working tree clean. Clean means all three zones match: your files, the staging area, and the newest snapshot are identical.

The everyday shortcut: commit -a

Once the edit→add→commit rhythm is second nature, typing git add for every routine edit gets old. git commit -a -m "..." (usually shortened to -am) tells Git to automatically stage every modification to files it already tracks, then commit in one step.

The fine print is the important part: -a only re-stages files that appear in an earlier commit. Brand-new files stay untracked and are NOT included — Git refuses to guess whether a file it has never seen belongs in history, so new files always need an explicit git add first. Engineers use -am dozens of times a day for small edits and fall back to the full add→commit two-step whenever new files or partial staging are involved.

Make your first commit. pancakes.txt is staged and waiting from lesson 2-2.

Step 1/2: Commit the staged file with the message: Add pancake recipe

~/recipe-book $ 

Quiz

You edited a.txt and b.txt, ran git add a.txt, then git commit -m "Update a". What is in that commit?

Problem

Fill in the blank so the commit gets a message without opening an editor: git commit ___ "Add waffle recipe"

Quiz

Match the flow from lesson 2-2's figure: which command moves changes from the staging area into the repository?

Quiz

You create a brand-new file notes.txt and also edit soup.txt (already in an earlier commit), then run git commit -am "Update". What ends up in the commit?