MODULE 6 · TOPIC 3 · VISUAL GIT LAB

Different States in Git

Git compares three snapshots: the last committed tree, the index and the working tree. An untracked file is not yet in the index. A tracked file can be unmodified, modified, staged or both staged and modified. Short status uses two columns: X compares index against HEAD and Y compares working tree against index. Unmerged states use special codes such as UU.

Interactive replays of recorded Git 2.47 command traces. The browser changes only the simulation; no local files, Git repositories or hosting accounts are modified. Commit graphs run from parent to child.

1. Tracked versus untracked

Tracking means a path is represented in the index or committed history. New files are untracked until added.

Why this example? Compare a tracked modification and an untracked file.

Before you run: Select a scenario and follow the Git execution diagram. Inspect the recorded file or reference in the Git workspace; switch between HEAD, Index and Working tree when available. The command and terminal panels show the exact replay. No Git commands execute in the browser.
Git workspaceInteractive replay
$
Source control
Snapshot inspector

Why this step happens
See all steps in this scenario
    Git Bash — commands and setup
    Git Bash — terminal outputRecorded output
    Repository stateCompact blocks
    CurrentResultError / rejected

    Change inputs and step through the example. Outputs are recorded from Git 2.47. Hashes, paths and configuration can differ on your computer. Run worked examples only in fresh practice folders.

    What this animation explains

    Tracking means a path is represented in the index or committed history. New files are untracked until added.

    Try and observe

    Look at the first and second columns of short status.

    2. Three versions of one file

    The same path may have three different contents at once: HEAD, index and working copy. Git status reports both differences.

    Why this example? Compare staged-only and staged-plus-edited files.

    Before you run: Select a scenario and follow the Git execution diagram. Inspect the recorded file or reference in the Git workspace; switch between HEAD, Index and Working tree when available. The command and terminal panels show the exact replay. No Git commands execute in the browser.
    Git workspaceInteractive replay
    $
    Source control
    Snapshot inspector

    Why this step happens
    See all steps in this scenario
      Git Bash — commands and setup
      Git Bash — terminal outputRecorded output
      Repository stateCompact blocks
      CurrentResultError / rejected

      Change inputs and step through the example. Outputs are recorded from Git 2.47. Hashes, paths and configuration can differ on your computer. Run worked examples only in fresh practice folders.

      What this animation explains

      The same path may have three different contents at once: HEAD, index and working copy. Git status reports both differences.

      Try and observe

      Inspect git show :app.txt and the working file.

      3. Diff comparison modes

      A patch depends on which two snapshots are compared. Default diff does not include staged-only changes.

      Why this example? Choose comparison target with all three versions present.

      Before you run: Select a scenario and follow the Git execution diagram. Inspect the recorded file or reference in the Git workspace; switch between HEAD, Index and Working tree when available. The command and terminal panels show the exact replay. No Git commands execute in the browser.
      Git workspaceInteractive replay
      $
      Source control
      Snapshot inspector

      Why this step happens
      See all steps in this scenario
        Git Bash — commands and setup
        Git Bash — terminal outputRecorded output
        Repository stateCompact blocks
        CurrentResultError / rejected

        Change inputs and step through the example. Outputs are recorded from Git 2.47. Hashes, paths and configuration can differ on your computer. Run worked examples only in fresh practice folders.

        What this animation explains

        A patch depends on which two snapshots are compared. Default diff does not include staged-only changes.

        Try and observe

        Observe the removed and added lines.

        4. Unstage a change

        git restore --staged updates the index from HEAD by default without changing the working file.

        Why this example? Unstage a modified file or a newly added path.

        Before you run: Select a scenario and follow the Git execution diagram. Inspect the recorded file or reference in the Git workspace; switch between HEAD, Index and Working tree when available. The command and terminal panels show the exact replay. No Git commands execute in the browser.
        Git workspaceInteractive replay
        $
        Source control
        Snapshot inspector

        Why this step happens
        See all steps in this scenario
          Git Bash — commands and setup
          Git Bash — terminal outputRecorded output
          Repository stateCompact blocks
          CurrentResultError / rejected

          Change inputs and step through the example. Outputs are recorded from Git 2.47. Hashes, paths and configuration can differ on your computer. Run worked examples only in fresh practice folders.

          What this animation explains

          git restore --staged updates the index from HEAD by default without changing the working file.

          Try and observe

          Observe that file content survives.

          5. Restore working content

          git restore app.txt uses the index as source by default. Specifying --source=HEAD selects the last commit instead. Both can overwrite unstaged content.

          Why this example? Compare restoring from index and HEAD in a disposable repo.

          Before you run: Select a scenario and follow the Git execution diagram. Inspect the recorded file or reference in the Git workspace; switch between HEAD, Index and Working tree when available. The command and terminal panels show the exact replay. No Git commands execute in the browser.
          Git workspaceInteractive replay
          $
          Source control
          Snapshot inspector

          Why this step happens
          See all steps in this scenario
            Git Bash — commands and setup
            Git Bash — terminal outputRecorded output
            Repository stateCompact blocks
            CurrentResultError / rejected

            Change inputs and step through the example. Outputs are recorded from Git 2.47. Hashes, paths and configuration can differ on your computer. Run worked examples only in fresh practice folders.

            What this animation explains

            git restore app.txt uses the index as source by default. Specifying --source=HEAD selects the last commit instead. Both can overwrite unstaged content.

            Try and observe

            Observe what happens to version 3 and staged version 2.

            6. Conflict state and index entries

            During a conflicting three-way merge, the index can hold base, ours and theirs entries instead of a normal stage-0 entry. Git reports an unmerged path.

            Why this example? Compare an overlapping merge and a clean merge.

            Before you run: Select a scenario and follow the Git execution diagram. Inspect the recorded file or reference in the Git workspace; switch between HEAD, Index and Working tree when available. The command and terminal panels show the exact replay. No Git commands execute in the browser.
            Git workspaceInteractive replay
            $
            Source control
            Snapshot inspector

            Why this step happens
            See all steps in this scenario
              Git Bash — commands and setup
              Git Bash — terminal outputRecorded output
              Repository stateCompact blocks
              CurrentResultError / rejected

              Change inputs and step through the example. Outputs are recorded from Git 2.47. Hashes, paths and configuration can differ on your computer. Run worked examples only in fresh practice folders.

              What this animation explains

              During a conflicting three-way merge, the index can hold base, ours and theirs entries instead of a normal stage-0 entry. Git reports an unmerged path.

              Try and observe

              Inspect status and ls-files -u before resolving.

              Git command reference

              Use these commands in a disposable practice repository. Review the output before running commands that change files or references.

              Git commandWhy and when to use it
              git status --shortRead two-column state
              git diffWorking tree versus index
              git diff --cachedIndex versus HEAD
              git diff HEADWorking tree versus HEAD for tracked paths
              git restore --staged app.txtUnstage while keeping file content
              git restore app.txtRestore working file from index

              Worked practical examples

              Follow these procedures with the Git 2.47 teaching repository (Git Bash, Linux or macOS). Start each example in a separate new practice folder; never run destructive file commands in an important repository.

              1. Observe all three versions

              Procedure

              1. Start in a disposable repository.
              2. Stage version 2 and edit to version 3.
              3. Compare unstaged and staged diffs.
              4. Commit only the staged version.
              Code / configuration
              git init -q -b main
              git config user.name Student
              git config user.email student@example.invalid
              printf "version 1\n" > app.txt
              git add app.txt
              git commit -qm "Initial app"
              printf "version 2\n" > app.txt
              git add app.txt
              printf "version 3\n" > app.txt
              git status --short
              git diff
              git diff --cached
              git commit -m "Version 2"
              Expected result: HEAD and index hold version 2; working tree holds version 3.

              2. Unstage without losing work

              Procedure

              1. Start with staged version 2.
              2. Inspect status.
              3. Unstage the file and inspect it again.
              4. Read the file to confirm version 2 remains.
              Code / configuration
              git init -q -b main
              git config user.name Student
              git config user.email student@example.invalid
              printf "version 1\n" > app.txt
              git add app.txt
              git commit -qm "Initial app"
              printf "version 2\n" > app.txt
              git add app.txt
              git status --short
              git restore --staged app.txt
              git status --short
              cat app.txt
              Expected result: The file remains modified in the working tree.

              Predict the outcome before you run

              Try answering these questions, then expand each answer and repeat the appropriate simulation.

              1. Predict the outcome: New untracked file in Tracked versus untracked. What changes and why?

              First column A indicates staged addition. Inspect the final state in the lab.

              2. Predict the outcome: Working vs HEAD in Diff comparison modes. What changes and why?

              Reading a diff does not change files or index. Inspect the final state in the lab.

              3. Predict the outcome: Committed HEAD in Restore working content. What changes and why?

              Compare working contents with index and HEAD. Inspect the final state in the lab.

              Hands-on practice and assessment

              Git practice challenge

              Practice the status matrix, stage an edit, edit again, unstage and restore a disposable file.

              Edit this command scratchpad. Review the commands and execute only in a disposable Git practice repository; the browser does not run shell commands.

              Quick assessment

              Key points and real Git notes

              Restore can discard local content. Always inspect diff first. In examples, all files are disposable; do not run destructive commands on valuable files.