Course contents

Stage and commit your first change

Sign in to run this lab and save your progress

Learn

In the last lesson you turned a folder into a Git repo. But the repo is empty, no history yet. Now you'll record your first commit.

A commit is a snapshot of your project at a moment in time. Making one is a two step operation, and this is the part of Git that trips up almost every newcomer, so it's worth going slow.

The three states of a file

Every file in a Git repo lives in one of three places:

  1. The working directory. What's actually on disk. The files you can edit in your editor right now.
  2. The staging area (also called "the index"). A holding pen for changes you want to include in the next commit.
  3. The repository. The permanent history of commits.

git add moves a file from the working directory into the staging area. git commit takes everything currently staged and records it as a new commit in the repository.

Two commands, two moves. Sounds like extra work. It isn't.

Why staging exists

Staging lets you compose a commit. Imagine you've been working for an hour and touched five files, but only three of them belong together as a single logical change. With a staging area you can:

git add file-a.txt file-b.txt file-c.txt
git commit -m "Add A, B, C"

Then stage and commit the remaining two separately. Without staging, Git would have to commit everything you've touched or nothing. Staging gives you control over what goes into each snapshot of history.

Making your first commit

Say you have a README.md file sitting untracked in the folder. Three commands:

git add README.md
git commit -m "Add README"

Now git log --oneline will show one commit.

The -m flag lets you write the message inline. Without it, Git opens your default editor and asks you to type a message there. -m is faster for short messages.

Writing a good commit message

Two rules that will serve you well forever:

  • Imperative mood. Write "Add README", not "Added README" or "Adds the README". Think of the message as completing the sentence "If applied, this commit will ___".
  • Short and specific. "Fix typo in setup instructions" beats "fixes" or "wip". Future you will read these when hunting for a bug and thank present you for the specificity.

Skip both rules and Git will let you. Your future self is the one who suffers.

Your task

Your working directory contains an untracked file README.md. Initialize a Git repository, stage the file, and commit it with a meaningful message. After this, git log --oneline should show exactly one commit on the default branch.

Starter files

Preview only — open the lab to edit and run.

files_dir
02-first-commit-starter