Chapter DGit basicsPage 1 of 8

Git basics

Define the lab goal and success criteria

Git stores snapshots of tracked files as commits; the staging area lets you choose exactly which changes form the next snapshot.

~14 minLab goal

1Try it yourself

Playground

Git time machine

Walk commit → branch → break → recover → PR — one step at a time.

1 / 5

  • 1. Make a commit
  • 2. Create a branch
  • 3. Break a file (oops)
  • 4. Recover with checkout / reset
  • 5. Open a PR

Stage a focused change and commit with a clear why.

Before you start

Why this matters

Before running anything, predict one observable result from the case: an evaluation script and README are being changed together, but only the tested evaluation change should enter the first commit. Write the prediction beside the command or code line that should cause it. This makes the session an experiment rather than a transcription exercise.

2Learn the idea

Read

Explain the chapter step

Begin by writing the success condition in observable terms. For this case, success is not familiarity with the vocabulary; it is producing a small repository with a focused commit named Add held-out model evaluation and a clean working tree. Record the starting state so you can distinguish an improvement from a result that was already present.

On this page, the practical job is to state a measurable outcome before changing anything. The running case is an evaluation script and README are being changed together, but only the tested evaluation change should enter the first commit.

Read

Run the working example

git status --short
git diff -- train.py
git add train.py
git diff --cached
git commit -m "Add held-out model evaluation"
git log -1 --oneline

Expected evidence:

M  train.py
 M README.md
[main a1b2c3d] Add held-out model evaluation
 1 file changed, 6 insertions(+)

The output may include version-specific details such as hashes, paths, fitted thresholds, or final decimal places. Compare the structural facts described here rather than copying placeholders. If the structure differs, stop and inspect the earliest unexpected line.

Read

Read it line by line

  1. git status --short uses two columns: the left is staged and the right is unstaged.
  2. git diff inspects unstaged content, while git diff --cached inspects the proposed commit.
  3. git add train.py copies that file's current change into the index; it does not upload anything.
  4. git commit creates local history, and git log -1 identifies the resulting snapshot.

These lines form one chain: the working-tree versions of train.py and README.md becomes a commit containing the intended train.py diff while the README edit remains unstaged. Change only one input first. When several values change together, you cannot tell which change caused the new behavior.

Read

Common errors and fixes

  • First failure: fatal: not a git repository means initialize the intended folder with git init or change into the repository. Re-run the smallest command that proves the repair.
  • Second failure: an accidental staged secret must be removed from the index with git restore --staged FILE and from the file before committing. Preserve the failing input as a test when it represents a realistic mistake.
  • Misleading success: a commit that says nothing to commit has no staged changes; inspect both status columns instead of repeating the command. A clean-looking final line cannot cancel contradictory intermediate evidence.

When debugging, copy the exact error text and inspect names, paths, shapes, types, and versions. Explain the cause in one sentence before changing code. That discipline prevents a guessed repair from creating a second defect.

Read

Evidence for this stage

Use git show --stat --oneline HEAD and git show HEAD -- train.py. Success means the commit names one coherent change, contains no credential or generated environment, and leaves only the deliberate README edit in the working tree.

For the current explain step, save the smallest useful evidence: the relevant command, its output, and the input that produced it. Do not use a screenshot as the only record when text can be copied and searched. Keep generated artifacts separate from source inputs so rerunning the example does not destroy the evidence it is meant to evaluate.

Share the commit hash and the exact verification command. A teammate should be able to inspect the patch with git show HASH, run the relevant test, and understand why the snapshot exists.

Read

Reflect on the result

Return to your opening prediction. Mark it correct or rewrite it with the condition you missed. Then explain the difference between a successful execution and a trustworthy result for this specific example.

Checking tutor…

Continue learning · glossary & guides
  1. Which line or command establishes the current step's most important fact?
  2. What output would reveal that fatal: not a git repository means initialize the intended folder with git init or change into the repository?
  3. Can a new user reproduce a small repository with a focused commit named Add held-out model evaluation and a clean working tree from the stated setup?