Workflow#

sequenceDiagram
    participant SS as Stash Stack
    participant WD as Workspace<br>(Working Directory)
    participant ST as Staging Area<br>(Index)
    participant LR as Local Repository
    participant RR as Remote Repository

    Note over WD, RR: Forward Flow
    WD->>ST: git add <path>...
    WD->>ST: git rm <path>... (≈ rm + git add)
    ST->>ST: git rm --cached <path>... (untrack, keep in WD)
    ST->>LR: git commit -m <message>
    WD->>LR: git commit -a -m <message>
    LR->>RR: git push<br/>[<remote>] [<branch>]

    Note over WD, SS: Stashing Changes
    WD->>SS: git stash [push] [-u] -k
    ST->>SS: git stash [push] [-u]
    SS-->>WD: git stash pop
    SS-->>ST: git stash pop --index

    Note over WD, RR: Git Restore, Switch, and Reset
    ST-->>WD: git restore <path>...
    LR-->>ST: git restore --staged <path>...
    LR-->>WD: git restore<br/>--source=<commit> <path>...
    LR->>WD: git switch [-c] <branch>
    LR->>LR: git reset [--soft|--mixed|--hard]
    LR-->>ST: git reset [--mixed|--hard]
    LR-->>WD: git reset --hard
    LR-->>ST: git revert <commit>
    LR->>LR:
    LR-->>WD:

    Note over WD, RR: Remote Sync, Rebase, and Diff
    RR->>LR: git fetch [<remote>]
    RR->>WD: git pull [--rebase]<br/>[<remote>] [<branch>]
    LR->>LR: git rebase<br/><upstream> [<branch>]
    Note over WD, ST: git diff [<path>...]
    Note over ST, LR: git diff --staged [<path>...]
    Note over WD, LR: git diff <commit> [<path>...]

Concepts#

  • checkout: The act of restoring or switching the Workspace (WD) to the state of a specific commit stored in the Local Repository (LR).

  • branch & tag: Both are pointers to a specific commit.

    • branch: A dynamic pointer that automatically moves forward when new commits are made while checked out.
    • tag: A static pointer that remains at a specific commit (typically used for releases).
  • HEAD: A pointer to the current branch reference (attached) or a specific commit (detached).

    • It determines what is currently checked out in the Workspace (WD).
    • attached HEAD: The standard state where HEAD points to a branch. When you commit, the branch pointer moves forward automatically along with HEAD.
    • detached HEAD: A state where HEAD points directly to a commit instead of a branch. Work here can be lost if you switch branches without creating a new one (e.g., git switch -c <new-branch>).
    • git switch <branch>: Moves HEAD to point to the specified branch and updates the WD and ST to match.
  • index: Also known as the staging area or cache; stores changes to be included in the next commit.

    • --staged / --cached
      • git restore --staged <path>...: Unstages changes while keeping the WD intact (restores ST from LR).
      • git rm --cached <path>...: Stops tracking the file but keeps it in the WD.
      • git diff --staged: Compares the staging area with the LR (HEAD).
  • ref: A name that points to a specific commit, stored in the .git/refs/ directory.

    • branch: refs/heads/<branch>
    • tag: refs/tags/<tag>
    • remote branch: refs/remotes/<remote>/<remote-branch>
  • refspec: A mapping used by fetch and push to synchronize references between repositories. The format is typically [+]<src>:<dst>.

    • eg. git fetch origin master:refs/remotes/origin/master
  • staging, unstaging, discarding

    • staging (WD -> ST): Copying changes from the Working Directory to the Staging Area (git add).
    • unstaging (LR -> ST): To revert git add, we must restore the Staging Area from the Local Repository.
      • Before staging: v1 v0 v0 (WD ST LR)
      • After staging: v1 v1 v0 (WD ST LR)
      • Unstaging: v1 v0 <- v0 (restores ST from LR)
      • Note: If unstaging were ST -> WD, it would copy v1 back to the folder, leaving the file still “staged”.
    • discarding (ST -> WD): Overwriting the Working Directory with the version currently in the Staging Area (git restore). This effectively throws away any changes in the folder that haven’t been staged.