Hone

Lessons · Git · choosing what goes in the commit

Choosing what the next commit contains

git add copies a file's current state into the staging area. add chooses; commit records. Two steps, on purpose.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

Two unrelated fixes in one working tree can become two clean commits, because you stage one, commit, stage the other, commit. Without the staging step every commit would be 'everything I touched'.

How to think about it

Stage what belongs together. A file, a folder, or with -p a single hunk at a time. Then read git diff --staged, which prints exactly what you have staged: that is the commit you are about to make. It is the next lesson, so it is fine if you have not met it yet.

Worked example

git add a.txt
Stages one file as it is right now.
git add .
Stages everything under this folder, new and modified.
git add -p
Asks about each hunk: yes or no, so one file can feed two commits.
git status --short
A or M in the first column: what the next commit will contain.

Your turn

Stage the report for the next commit.

git  report.py

The trap

add records the file as it is at that moment. Edit it again and the new edit is not staged; git status shows MM until you add again.

Practise choosing what goes in the commit on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.