1. Separate saved, staged and committed content
By the end
- Identify the working file, index and current commit as three different states.
- Choose the diff that answers what the next commit will contain.
Start in a new empty folder, not an existing project. Run git init --initial-branch=main. The name and email below are local example identity settings; they are stored in this practice repository, not your global Git configuration. No network operation follows.
Saving a file changes the working tree. git add captures the chosen file content in the index. A normal git commit records that staged content. Editing the file again does not update the index automatically. Think of staging as selecting a version, not adding a permanent watch on a file.
git diff compares working content with the index; git diff --cached compares the index with the current commit. Use both before committing. A clean git diff alone does not prove there are no staged changes. Review the actual lines as well as the list of filenames.
Worked example
Create plan.txt containing daily_minutes=20 and a final newline. Stage it, then edit the working file to daily_minutes=30 without staging again. Predict the first commit.
- Initialize the empty folder and set the example identity locally.
- Save 20, run git add, then use your editor to save 30.
- git show :plan.txt reads the staged version and still shows 20. The ordinary diff shows 20 changing to 30.
- Commit without -a. HEAD contains 20; the working file remains 30 and has an uncommitted change.
git init --initial-branch=main
git config user.name "Learning Fixture"
git config user.email "learner@example.invalid"
# Save plan.txt with daily_minutes=20 before the next command.
git add -- plan.txt
# Now edit and save daily_minutes=30.
git show :plan.txt
git diff -- plan.txt
git diff --cached -- plan.txt
git commit -m "Start study plan"
git show HEAD:plan.txt
git status --shortThe commit contains 20, not 30. Staging again is necessary if the intended snapshot is the newer content.
Try it yourself
Finish the intended change to 30. Stage only plan.txt, inspect its staged diff and commit it. Explain why git diff --cached is empty afterward.
- HEAD and the working file both contain daily_minutes=30.
- git status --short and both diff commands produce no changes.
Reveal the practice solution
Run git add -- plan.txt, git diff --cached -- plan.txt, then git commit -m "Use 30 minute sessions". The index now matches the new HEAD, so there is no staged difference. The working file also matches.
Watch for this mistake: git add does not include edits made after that command. Inspect the staged version instead of assuming a saved file is committed.
Sources and review date
- Git: git-add
The index records the file contents added, independently of later working file edits. Checked: .
- Git: git-diff
Working tree, cached changes and three-dot merge-base comparisons. Checked: .
- Git: git-status
Inspect staged, unstaged and unmerged paths. Checked: .
Explanations, examples and quiz questions are original KitForma material. These links support technical facts and curriculum alignment.