Select and record a version

Lesson 3 of 25 · Git: records, branches, and collaboration

Continue in the disposable repository. Git separates the editable working directory, the index (also called the staging area), and the most recent commit. The index is the proposed snapshot for the next commit.

working file -- git add --> index -- git commit --> recorded snapshot
    edit it                 selected content          history

Create a file and inspect it. > creates or overwrites a file, so use it only in this new example directory; printf writes the given text and \n means a newline.

printf '# Garden guide\n' > guide.md
git status
git add guide.md
git status

At first, guide.md is untracked: it is not in the index. After git add, its current content is staged. Now change the working copy without staging again:

printf 'Water in the morning.\n' >> guide.md
git status
git diff
git diff --cached

>> appends. The path appears both staged and modified: the index still contains only the heading, while the working file also has the watering line. git diff compares working content to the index; git diff --cached compares the index to the latest commit, or shows staged content when there is no commit yet. A + line in a patch is an addition; a - line is a removal. Output and colors depend on configuration.

Stage the newer content and commit it:

git add guide.md
git diff --cached
git commit -m "Start garden guide"
git status

The message explains the purpose of the snapshot. A successful commit records the index, including previously tracked files that have not changed; it does not automatically include edits made after staging. Status should now say the working tree is clean in this example. Clean status is not proof of a backup, and ignored files can be present without appearing as untracked. git add . can stage many eligible paths under the current directory, including removals; inspect them first rather than accidentally including unrelated content.

Next: Keep generated files out.