Git 基础概念

graph LR
    classDef process fill:#E5F6FF,stroke:#73A6FF,stroke-width:2px;
    classDef storage fill:#FFF6CC,stroke:#FFBC52,stroke-width:2px;
    
    subgraph Remote["远程仓库"]
        A([Remote]):::storage
    end
    
    subgraph Local["本地仓库"]
        B[(Repository)]:::storage
        C[[Index]]:::process
        D([工作区 main tree]):::process
    end
    
    A ==>|fetch/clone| B
    B ==>|push| A
    D -.->|add| C
    C -.->|commit| B
    B -.->|checkout| D
    A ==>|pull| D
    
    linkStyle 0,1,5 stroke:#333,stroke-width:2px,fill:none
    linkStyle 2,3,4 stroke:#999,stroke-width:1.5px,stroke-dasharray:5,5    

image.png

submodule 子模块(可以自定义一个主模块,然后多个子模块,方便 llm 进行前后端协同工作)

worktree

多任务并行处理,实现一顶三

image.png

graph LR
    classDef process fill:#E5F6FF,stroke:#73A6FF,stroke-width:2px;
    classDef storage fill:#FFF6CC,stroke:#FFBC52,stroke-width:2px;
    
    subgraph Remote["远程仓库"]
        A([Remote]):::storage
    end
    
    subgraph Local["本地仓库"]
        B[(Repository)]:::storage
        C[[Index]]:::process
        D([工作区 main tree]):::process
        E([worktree 1]):::process
        F([worktree 2]):::process
        G([worktree ...]):::process
    end
    
    A ==>|fetch/clone| B
    B ==>|push| A
    D -.->|add| C
    C -.->|commit| B
    B -.->|checkout| D
    D <-->|worktree| E
    D <-->|worktree| F
    D <-->|worktree| G
    A ==>|pull| D
    
    linkStyle 0,1,8 stroke:#333,stroke-width:2px,fill:none 
    linkStyle 2,3,4 stroke:#999,stroke-width:1.5px,stroke-dasharray:5,5
    linkStyle 5,6,7 stroke:#666,stroke-width:1.5px,stroke-dasharray:3,3

理解 Git worktrees

假设您需要同时处理多个任务,并在 Claude Code 实例之间完全隔离代码。

https://docs.anthropic.com/zh-CN/docs/claude-code/common-workflows#使用-git-worktrees-运行并行-claude-code-会话

Git worktrees 允许您将同一存储库的多个分支检出到单独的目录中。每个 worktree 都有自己的工作目录和隔离的文件,同时共享相同的 Git 历史。在官方 Git worktree 文档中了解更多。

创建新的 worktree

# 使用新分支创建新的 worktreegit worktree add ../project-feature-a -b feature-a
# 或使用现有分支创建 worktreegit worktree add ../project-bugfix bugfix-123

这会创建一个新目录,其中包含您存储库的单独工作副本。

在每个 worktree 中运行 Claude Code

# 导航到您的 worktreecd ../project-feature-a
# 在这个隔离环境中运行 Claude Codeclaude

在另一个 worktree 中运行 Claude