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.
Three areas
- Working directory: your files as you edit them.
- Staging area (the index): the changes you've chosen for the next commit, with
git add. - 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.
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?
Creates a commitStages changes so they'll be included in the next commitUploads to GitHubCreates a new file
Show answer
Answer: Stages changes so they'll be included in the next commit