---
title: 版本控制集成
description: 配合 Git 及其他版本控制系统工作
---

Verdent for VS Code 与 Git 及其他版本控制系统无缝集成，支持自然语言版本控制操作、自动生成提交信息以及智能分支管理。本指南将展示如何利用 Verdent 的 Git 集成来实现高效的版本控制工作流。

---

## 生成有意义的提交信息

假设你已经做了修改，并希望 Verdent 生成一条描述性的提交信息。

<Steps>
  <Step title="请求提交并生成提交信息">
    ```
    Stage all changes and create a commit with an appropriate message
    ```

    Verdent 使用 git diff 分析你的修改。
  </Step>

  <Step title="Verdent 分析修改">
    Verdent 会检查：
    - 修改的文件及其用途
    - 修改的性质（新功能、缺陷修复、重构）
    - 影响范围
    - 相关功能
  </Step>

  <Step title="生成描述性提交信息">
    ```bash
    git commit -m "feat: add user profile image upload with S3 integration

    - Add file upload endpoint to user API
    - Integrate AWS S3 for image storage
    - Update user model with profileImage field
    - Add frontend image upload component with preview"
    ```

    提交信息遵循约定式提交格式，并描述修改了什么内容。
  </Step>

  <Step title="创建提交">
    修改将以生成的提交信息进行提交。你可以查看该提交：

    ```bash
    git log -1
    ```
  </Step>
</Steps>

<Tip>
  **提示：**
  - Verdent 遵循约定式提交格式（feat、fix、refactor、docs 等）
  - 提交信息侧重于"做了什么"和"为什么"，而非"怎么做"
  - 你可以在用户规则或项目规则中自定义提交信息格式
  - 请求特定的提交信息风格："创建一个带有详细多行信息的提交"
</Tip>

***

## 自定义提交信息格式

假设你希望 Verdent 遵循你团队特定的提交信息约定。

<Tabs>
  <Tab title="用户规则（全局）">
    在 `VERDENT.md` 中为所有项目定义提交信息偏好：

    ```markdown
    # VERDENT.md

    ## Git Commit Messages

    When generating commit messages:
    - Always include ticket number in format: [PROJ-123]
    - Use present tense verbs
    - Maximum 50 characters for first line
    - Include detailed explanation in body
    - Add "Co-authored-by" for pair programming sessions

    Example format:
    [PROJ-123] Add user authentication feature

    Detailed explanation of changes...

    Co-authored-by: Team Member <email@example.com>
    ```

    Verdent 会在全局范围内遵循这些规则。
  </Tab>

  <Tab title="项目规则">
    在 `AGENTS.md` 中定义项目特定的提交约定：

    ```markdown
    # AGENTS.md

    ## Git Commit Conventions

    For this project, use conventional commits with these scopes:
    - feat(api): API changes
    - feat(ui): Frontend changes
    - fix(auth): Authentication fixes
    - docs(readme): Documentation updates

    Always reference GitHub issue: "Fixes #123" or "Relates to #456"
    ```

    这些规则仅适用于该项目。
  </Tab>

  <Tab title="内联指令">
    直接提供一次性指令：

    ```
    Create a commit with message format: "[TICKET-NUMBER] description" including reference to issue #42
    ```

    Verdent 生成：
    ```bash
    git commit -m "[PROJ-42] Add search functionality

    Relates to #42"
    ```
  </Tab>
</Tabs>

<Tip>
  **提示：**
  - 用户规则在所有项目中全局生效
  - 项目规则（AGENTS.md）会针对特定项目覆盖用户规则
  - 内联指令会覆盖两者，用于一次性需求
  - 推荐使用约定式提交格式以保持一致性
</Tip>

***

## 创建拉取请求

假设你希望 Verdent 创建一个完整的拉取请求。

<Steps>
  <Step title="确保修改已提交">
    ```
    Make sure all my changes are committed
    ```

    Verdent 会检查 git status 并提交任何未提交的修改。
  </Step>

  <Step title="将分支推送到远程">
    ```
    Push this branch to origin
    ```

    Verdent 推送：
    ```bash
    git push origin feature/user-notifications
    ```
  </Step>

  <Step title="请求创建 PR">
    ```
    Create a pull request for this feature
    ```

    Verdent 使用 `gh` CLI 来创建 PR。
  </Step>

  <Step title="Verdent 生成 PR 描述">
    Verdent 分析提交和修改来生成：

    **标题：** Add user notification system

    **正文：**
    ```markdown
    ## Summary
    - Implements real-time user notifications
    - Adds WebSocket support for instant delivery
    - Includes email fallback for offline users
    - Comprehensive test coverage

    ## Test Plan
    - [ ] Test real-time notifications in browser
    - [ ] Verify email fallback for offline users
    - [ ] Check notification persistence across sessions
    - [ ] Test with multiple concurrent users

    ## Changes
    - Added NotificationService with WebSocket integration
    - Created notification UI components
    - Integrated SendGrid for email notifications
    - Added notification preferences to user settings
    ```

    PR 将以完整的描述创建。
  </Step>
</Steps>

<Tip>
  **提示：**
  - Verdent 会分析分支中的所有提交来生成 PR 描述
  - 请求特定的 PR 格式："创建一个带有详细测试计划的 PR"
  - 包含截图："将此截图添加到 PR 描述中"
  - 你可以在创建前完善 PR 描述："更新 PR 以提及这个破坏性变更"
</Tip>

***

## 解决合并冲突

假设你遇到了合并冲突，需要 Verdent 帮助解决。

<Steps>
  <Step title="尝试合并">
    ```
    Merge main into this feature branch
    ```

    出现合并冲突：
    ```bash
    Auto-merging src/auth.ts
    CONFLICT (content): Merge conflict in src/auth.ts
    ```
  </Step>

  <Step title="请求解决冲突">
    ```
    Help me resolve the merge conflict in src/auth.ts
    ```

    Verdent 读取冲突标记。
  </Step>

  <Step title="Verdent 分析两个版本">
    Verdent 会检查：
    - 当前分支的修改（HEAD）
    - 传入的修改（main 分支）
    - 冲突周围的上下文
    - 两处修改的意图
  </Step>

  <Step title="Verdent 提出解决方案">
    ```
    The conflict is between your JWT implementation and the main branch's session-based auth. I'll merge both approaches to support both authentication methods.
    ```

    Verdent 通过智能整合两处修改来解决冲突。
  </Step>

  <Step title="将冲突标记为已解决">
    ```bash
    git add src/auth.ts
    git commit -m "Merge main into feature/jwt-auth, resolved conflicts"
    ```

    冲突已解决，合并完成。
  </Step>
</Steps>

<Tip>
  **提示：**
  - Verdent 理解代码上下文，能够智能地解决冲突
  - 在提交前务必检查冲突解决方案
  - 对于复杂的冲突，可先让 Verdent 解释两个版本
  - 解决冲突后请充分测试
</Tip>

<Tip>
Verdent 通过理解两个分支的意图来分析合并冲突，提出能够保留双方功能的解决方案。
</Tip>

***

## 管理分支和标签

假设你需要管理分支并创建发布标签。

<Tabs>
  <Tab title="分支操作">
    **创建并切换分支：**

    ```
    Create a new branch called feature/user-notifications
    ```

    Verdent 执行：

    <CodeGroup>
    ```bash "Create New Branch"
    git checkout -b feature/user-notifications
    ```

    ```bash "Switch to Existing Branch"
    git checkout main
    ```

    ```bash "Create and Push Branch"
    git checkout -b feature/payment-integration
    git push -u origin feature/payment-integration
    ```
    </CodeGroup>
  </Tab>

  <Tab title="合并">
    **合并功能分支：**

    ```
    Merge the feature/user-notifications branch into main
    ```

    Verdent 执行合并工作流：
    ```bash
    git checkout main
    git pull origin main
    git merge feature/user-notifications
    git push origin main
    ```

    Verdent 会在合并前确保 main 是最新的。
  </Tab>

  <Tab title="为发布打标签">
    **创建带注释的标签：**

    ```
    Create an annotated tag for version 1.2.0 with release notes
    ```

    Verdent 创建详细标签：
    ```bash
    git tag -a v1.2.0 -m "Release 1.2.0

    New Features:
    - User notification system
    - Email integration
    - Real-time WebSocket support

    Bug Fixes:
    - Fixed authentication timeout issue
    - Resolved cart calculation bug"
    ```

    **推送标签：**

    ```
    Push all tags to origin
    ```

    Verdent 推送：
    ```bash
    git push origin --tags
    ```
  </Tab>
</Tabs>

<Tip>
  **提示：**
  - 使用描述性的分支名称：`feature/user-auth`、`fix/cart-bug`、`refactor/api-layer`
  - 合并前务必拉取最新修改
  - 为发布使用带注释的标签（它们包含元数据）
  - 遵循语义化版本：v1.2.3（主版本.次版本.修订版本）
</Tip>

<Tip>
一致的分支命名约定有助于 Verdent 理解你的工作流。在 AGENTS.md 中定义命名模式即可自动遵循。
</Tip>

***

## 常见问题

<Accordion title="Verdent 会自动提交我的修改吗？">
不会。Verdent 只在你明确请求时才创建提交。你完全掌控何时提交修改。准备好后只需说"暂存所有修改并创建提交"即可。
</Accordion>

<Accordion title="我可以在提交前编辑提交信息吗？">
可以。你可以让 Verdent 在创建提交前修改提交信息。比如说"更新提交信息以提及破坏性变更"或"让提交信息更简洁"。Verdent 会根据你的反馈重新生成信息。
</Accordion>

<Accordion title="Verdent 支持 GitHub、GitLab、Bitbucket 及其他 Git 平台吗？">
支持。Verdent 使用标准的 Git 命令，因此可与任何 Git 仓库配合使用，无论托管平台为何。对于创建拉取请求，Verdent 使用 `gh` CLI，这需要 GitHub，但所有其他 Git 操作都通用。
</Accordion>

<Accordion title="Verdent 会未经询问就推送到远程仓库吗？">
不会。Verdent 只在你明确请求时才推送到远程仓库。出于安全考虑，所有 Git 操作（提交、推送、合并、变基）都需要你明确指示。
</Accordion>

<Accordion title="Verdent 能解决所有类型的合并冲突吗？">
Verdent 能够通过理解代码上下文和意图来解决大多数基于文本的合并冲突。二进制文件冲突或非常复杂的多方冲突可能需要手动介入。在提交前请务必检查 Verdent 的冲突解决方案。
</Accordion>

***

## 另请参阅

<CardGroup cols={2}>
  <Card title="多步骤任务示例" icon="list-check" href="/docs/verdent-for-vscode/common-workflows/multi-step-tasks">
    复杂的多步骤工作流与任务管理
  </Card>

  <Card title="编写新代码" icon="code" href="/docs/verdent-for-vscode/task-based-guides/writing-code">
    使用 Verdent 创建新功能和组件
  </Card>
</CardGroup>
