Git & Command Line Tutorial · Git Basics · Lesson 9 of 20

Staging and Committing

The three areas of Git (working directory, staging area, commits), git status, git add, and writing commits with good messages.

Create a free account to save this page, take notes and track your progress. Sign in

Three areas

  1. Working directory: your files as you edit them.
  2. Staging area (the index): the changes you've chosen for the next commit, with git add.
  3. Repository: saved snapshots called commits, made with git commit.

Staging lets you commit related changes together even if you edited several things: git add file, git add folder/, git add -p (pick individual chunks), git add . (everything).

Good commit messages

A short summary line (about 50 characters, imperative mood: "Add login form", not "added stuff"), a blank line, then why the change was made if it isn't obvious. Small, focused commits are easier to review and to undo.

add, status and commit
git init -b main --quiet recipes && cd recipes
echo "Pancakes: flour, milk, eggs" > pancakes.txt
echo "Soup: water, vegetables" > soup.txt
git status --short            # ?? = untracked

git add pancakes.txt
git status --short            # A = staged (added)
git commit -m "Add pancake recipe" --quiet

echo "Soup: water, vegetables, salt" > soup.txt
echo "Pancakes: flour, milk, eggs, sugar" > pancakes.txt
git add soup.txt pancakes.txt
git status --short            # A = new file staged, M = modified file staged
git commit -m "Add soup recipe and sweeten pancakes" --quiet

git log --oneline

Output

?? pancakes.txt
?? soup.txt
A  pancakes.txt
?? soup.txt
M  pancakes.txt
A  soup.txt
3ad2879 Add soup recipe and sweeten pancakes
a101c44 Add pancake recipe

Run this on your computer — see “Get Started” for setup.

Test yourself

What does git add do?

  1. Creates a commit
  2. Stages changes so they'll be included in the next commit
  3. Uploads to GitHub
  4. Creates a new file
Show answer

Answer: Stages changes so they'll be included in the next commit

Was this page helpful?