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, 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>...]
  • Pro Git: 가장 포괄적인 Git 공식 가이드.
  • Learn Git Branching: 시각적으로 Git 명령어를 배울 수 있는 대화형 학습 사이트.

Concepts#

  • checkout: Local Repository(LR)에 저장된 특정 commit 상태를 Workspace(WD)로 복원하거나 전환하는 행위.
  • branch & tag: 둘 다 특정 commit을 가리키는 포인터.
    • branch: 동적(Dynamic) 포인터. 해당 브랜치가 체크아웃된 상태에서 새로운 커밋이 발생하면 자동으로 앞으로 이동한다.
    • tag: 정적(Static) 포인터. 특정 커밋에 고정되어 이동하지 않는다 (주로 릴리스 지점을 표시할 때 사용).
  • HEAD: 현재 branch reference(attached) 또는 특정 commit(detached)을 가리키는 포인터.
    • Workspace(WD)에 현재 checkout된 대상을 결정한다.
    • attached HEAD: HEAD가 branch를 가리키고 있는 일반적인 상태. 커밋 시 branch 포인터가 HEAD와 함께 자동으로 앞으로 이동한다.
    • detached HEAD: HEAD가 branch 대신 특정 commit을 직접 가리키는 상태. git switch -c <new-branch>와 같이 새 branch를 생성하지 않고 branch를 전환하면 작업 내용이 소실될 수 있다.
    • git switch <branch>: HEAD가 특정 branch를 가리키도록 이동시키고, WD와 ST를 해당 상태로 갱신한다.
  • index: staging area 또는 cache라고도 하며, 다음 commit에 포함될 변경 사항을 저장한다.
    • --staged / --cached
      • git restore --staged <path>...: staging area의 변경 사항을 취소한다 (LR에서 ST를 복원하며 WD는 유지).
      • git rm --cached <path>...: tracking을 중지하고 WD의 파일은 유지한다.
      • git diff --staged: staging area와 LR(HEAD)을 비교한다.
  • ref: 특정 commit을 가리키는 이름. .git/refs/ 디렉토리에 저장된다.
    • branch: refs/heads/<branch>
    • tag: refs/tags/<tag>
    • remote branch: refs/remotes/<remote>/<remote-branch>
  • refspec: fetch 및 push 시 로컬과 원격 저장소 간의 참조(ref)를 동기화하기 위한 매핑 규칙. 일반적으로 [+]<src>:<dst> 형식을 사용한다.
    • eg. git fetch origin master:refs/remotes/origin/master
  • staging, unstaging, discarding
    • staging (WD -> ST): 작업 디렉토리의 변경 사항을 Staging Area로 복사한다 (git add).
    • unstaging (LR -> ST): git add를 되돌리려면 Local Repository의 상태를 Staging Area로 가져와야 한다.
      • Staging 전: v1 v0 v0 (WD ST LR)
      • Staging 후: v1 v1 v0 (WD ST LR)
      • Unstaging: v1 v0 <- v0 (LR에서 ST를 복원)
      • 주의: 만약 unstaging이 ST -> WD라면, Stage의 v1을 폴더로 다시 복사하게 되어 파일은 여전히 “staged” 상태로 남게 된다.
    • discarding (ST -> WD): 현재 Staging Area에 저장된 버전으로 작업 디렉토리를 덮어쓴다 (git restore). 즉, 스테이징되지 않은 모든 변경 사항을 폐기한다.