Lifestyle

Git, branches, diffs and recovery

Git stores file history, branches organize changes and diffs show what changed. Codex can help, but you must verify that the changes belong to the task. Restoring an old conversation does not restore Git files.

About 15 min read · Practice 30 min

Workflow illustration, not a product screenshot.
Image: Mokaair (© Mokaair)
Back to directory:Codex learning hub: tutorial directory

Practical · Desktop / CLI / VS Code / JetBrains

On this page
  1. Goal and preparation
  2. Step 1: Create an isolated baseline
  3. Step 2: Make two distinguishable changes on a branch
  4. Step 3: Stage one file, then practice restoration
  5. Step 4: Commit the scoped edit and revert with history
  6. Command comparison, troubleshooting and acceptance

Goal and preparation

Know . The working tree is current files, the index is the next commit's staged content, and commits record versions; these can differ. A branch identifies a history line, not an automatic copy of uncommitted files. For separate directories, use ; creating a branch is not a backup.

Step 1: Create an isolated baseline

Place the five expected files in new codex-git-lab outside any other project. Open its editor terminal, verify the path with Get-Location in Windows PowerShell or pwd on macOS/Linux, then run git --version. If Git is missing, use its official platform installation guidance and reopen the terminal. Do not initialize your existing work repository for this exercise.

Terminal: isolated Git baseline · sh
git init -b main
git config user.name "Practice Learner"
git config user.email "learner@example.invalid"
git add index.html style.css app.js core.mjs core.test.mjs
git diff --cached --name-only
git commit -m "Add practice baseline"
git status --short

Run line by line. Initial staging should contain exactly five files and status should be clean after committing. Identity values are local to this practice repository, not GitHub login or global configuration. If existing signing/hooks requirements prevent a commit, follow your setup or record the blocker instead of changing global policy.

Step 2: Make two distinguishable changes on a branch

Terminal: create a branch · sh
git switch -c codex/title-lab
git branch --show-current

Verify codex/title-lab. In index.html change only the h1 text Make room for one small task. to Plan one useful step. Append the comment below to style.css to represent separate uncommitted work. The title is the deliverable; preserve the CSS note without including it in the title commit. Both edits are fictional practice.

Append to file: style.css · css
/* KEEP-MY-NOTE */
Terminal: inspect the diff · sh
git status --short
git diff -- index.html style.css

Minus lines are old, plus lines new, with surrounding context. Check the exact h1 and CSS comment instead of only “2 files changed.” Use the read-only prompt below for Codex to identify existing work and intended scope before any commit request.

Codex prompt: read-only scope check · text
Inspect this lab's current branch, status and diff without editing, staging or committing. The intended change is only the h1 title in index.html. The KEEP-MY-NOTE comment in style.css represents separate work and must be preserved. Explain what belongs in the title commit and what must stay outside it.

Step 3: Stage one file, then practice restoration

Terminal: stage only the intended file · sh
git add index.html
git diff --cached --name-only
git diff --cached -- index.html
git diff -- style.css

Now compare two versions of the same file. Without running git add again, change only the staged title Plan one useful step. to Compare one useful step. in the editor. Run the four lines below to distinguish the working tree, index and HEAD.

Terminal: staged and unstaged versions of one file · sh
git status --short
git diff --cached -- index.html
git diff -- index.html
git show :index.html

Expect MM index.html and unstaged style.css. This is not a conflict. In this case, the first M means the index differs from HEAD; the second means the working tree differs from the index, as explained in Git status. The cached diff should show Make → Plan, the plain diff Plan → Compare, and show :index.html the staged Plan version. An ordinary commit now would not automatically include Compare.

Next run the two restore commands in their original order. The first only unstages, leaving Compare on disk. The second restores index.html from the index, now containing the baseline title. If you skip the first command, worktree restore instead uses the still-staged Plan version. Git restore documents these default sources. Confirm that you intend to discard this practice title; CSS must remain untouched.

Terminal: unstage, then restore · sh
git restore --staged -- index.html
git restore --worktree -- index.html
git status --short
git diff -- style.css

Now only style.css should be modified, h1 restored and the note preserved. Undoing your change must not clear separate work. restore --staged does not discard the working-file edit; restore --worktree overwrites the specified file from its restore source. They are not interchangeable.

Step 4: Commit the scoped edit and revert with history

Reapply Plan one useful step. in the editor, stage only index.html and inspect cached diff again. The commit below should contain only the title while CSS remains uncommitted. Verify committed paths with show; command success alone does not prove correct scope.

Terminal: verify and commit the title · sh
git add index.html
git diff --cached --name-only
git diff --cached -- index.html
git commit -m "Update practice title"
git show --format=short --name-only HEAD
git status --short

Before reverting, resolve working-tree changes. Here the only remaining change is your fictional CSS note. After verifying its preservation, explicitly finish that exercise and discard only it with the first command below. Require clean status and confirm HEAD is Update practice title before reverting HEAD. Do not apply this cleanup to real work with unrelated changes.

Terminal: finish the note exercise and revert the commit · sh
git restore --worktree -- style.css
git status --short
git log -1 --oneline
git revert --no-edit HEAD
git log -3 --oneline
git status --short

revert adds a reversing commit while preserving the original title commit. Expect original h1, three history entries and clean status. On conflict, inspect actual state instead of forcing options. To cancel an unfinished revert, use Git's git revert --abort workflow and recheck files.

Command comparison, troubleshooting and acceptance

GoalCommandCheck
Inspect unstaged contentgit diffWorking tree versus index
Inspect next commitgit diff --cachedIndex versus HEAD
Unstagegit restore --staged -- index.htmlEdit remains
Discard selected uncommitted editgit restore --worktree -- index.htmlVerify source and path first
Reverse a commitgit revert --no-edit HEADNew history; verify target and clean state

For a missing branch, verify the repository. Inspect unexpected files instead of staging every file at once. If one file mixes edits, whole-file staging includes all of them; review hunks before selective handling. Acceptance proves CSS exclusion, distinct restore effects and preserved revert history. Diagram 1 identifies state, 2 scopes commits, 3 verifies restoration. Continue to .

19. Git, branches, diffs and recovery — Workflow illustration, not a product screenshot. Branch → Diff → Commit
19. Git, branches, diffs and recovery — Workflow illustration, not a product screenshot. Branch → Diff → Commit · Image: Mokaair (© Mokaair)
Read the full description

Branch to Diff to Commit

Back to directory

  • Lifestyle

    Codex learning hub: tutorial directory

    A planned 60-lesson, ten-unit Codex curriculum, from setup and your first task to MD instructions and advanced integrations. Find your next lesson by experience, platform, goal or command; unpublished entries show their status.

  • Lifestyle

    Workshop: build a small website

    Plan and build the Small Steps task website from brief.md, with adding, completing, deleting, filtering and local persistence. Separate HTML, CSS, data functions, UI events and tests, verify with Node and browser checks, and document restart and recovery steps.

  • Lifestyle

    Usage and efficiency: reducing rework

    Record task conditions, model options, time and outcomes to reduce unnecessary retries and excess context.

Latest travel guides

Sources

Lifestyle