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>...]
Recommended Resources#
- Pro Git: The comprehensive official guide.
- Learn Git Branching: Interactive visual learning for Git commands.
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
HEADpoints to a branch. When you commit, the branch pointer moves forward automatically along withHEAD. - detached HEAD: A state where
HEADpoints 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>: MovesHEADto 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/--cachedgit 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>
- branch:
refspec: A mapping used by
fetchandpushto synchronize references between repositories. The format is typically[+]<src>:<dst>.- eg.
git fetch origin master:refs/remotes/origin/master
- eg.
staging, unstaging, discarding
- staging (
WD -> ST): Copying changes from the Working Directory to the Staging Area (git add). - unstaging (
LR -> ST): To revertgit add, we must restore the Staging Area from the Local Repository.- Before staging:
v1v0v0(WD ST LR) - After staging:
v1v1v0(WD ST LR) - Unstaging:
v1v0<-v0(restores ST from LR) - Note: If unstaging were
ST -> WD, it would copyv1back to the folder, leaving the file still “staged”.
- Before staging:
- 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.
- staging (