Skip to content

检查点

¥Checkpointing

Gemini CLI 包含检查点功能,可在 AI 工具对文件进行任何修改之前自动保存项目状态快照。这样,您就可以安全地试验和应用代码更改,并且知道可以立即恢复到工具运行之前的状态。

¥The Gemini CLI includes a Checkpointing feature that automatically saves a snapshot of your project's state before any file modifications are made by AI-powered tools. This allows you to safely experiment with and apply code changes, knowing you can instantly revert back to the state before the tool was run.

工作原理

¥How It Works

当你批准修改文件系统的工具时(例如write_file或者replace),CLI 会自动创建一个“检查点”。此检查点包括:

¥When you approve a tool that modifies the file system (like write_file or replace), the CLI automatically creates a "checkpoint." This checkpoint includes:

  1. Git 快照:提交是在位于您的主目录 (~/.gemini/history/<project_hash>)。此快照捕获了当时项目文件的完整状态。它确实不是干扰您自己项目的 Git 存储库。

    ¥A Git Snapshot: A commit is made in a special, shadow Git repository located in your home directory (~/.gemini/history/<project_hash>). This snapshot captures the complete state of your project files at that moment. It does not interfere with your own project's Git repository.

  2. 对话历史记录:到目前为止,您与代理的整个对话都将被保存。

    ¥Conversation History: The entire conversation you've had with the agent up to that point is saved.

  3. 工具调用:即将执行的特定工具调用也被存储。

    ¥The Tool Call: The specific tool call that was about to be executed is also stored.

如果您想要撤消更改或只是返回,您可以使用/restore命令。恢复检查点将:

¥If you want to undo the change or simply go back, you can use the /restore command. Restoring a checkpoint will:

  • 将项目中的所有文件恢复到快照中捕获的状态。

    ¥Revert all files in your project to the state captured in the snapshot.

  • 在 CLI 中恢复对话历史记录。

    ¥Restore the conversation history in the CLI.

  • 重新提出原始工具调用,允许您再次运行它、修改它或简单地忽略它。

    ¥Re-propose the original tool call, allowing you to run it again, modify it, or simply ignore it.

所有检查点数据(包括 Git 快照和对话历史记录)都存储在您的本地计算机上。Git 快照存储在影子存储库中,而对话历史记录和工具调用则保存在项目临时目录中的 JSON 文件中,该文件通常位于~/.gemini/tmp/<project_hash>/checkpoints

¥All checkpoint data, including the Git snapshot and conversation history, is stored locally on your machine. The Git snapshot is stored in the shadow repository while the conversation history and tool calls are saved in a JSON file in your project's temporary directory, typically located at ~/.gemini/tmp/<project_hash>/checkpoints.

启用该功能

¥Enabling the Feature

检查点功能默认处于禁用状态。要启用此功能,您可以使用命令行标志或编辑您的settings.json文件。

¥The Checkpointing feature is disabled by default. To enable it, you can either use a command-line flag or edit your settings.json file.

使用命令行标志

¥Using the Command-Line Flag

您可以使用--checkpointing启动 Gemini CLI 时的标志:

¥You can enable checkpointing for the current session by using the --checkpointing flag when starting the Gemini CLI:

gemini --checkpointing

使用settings.json文件

¥Using the settings.json File

要为所有会话默认启用检查点,您需要编辑settings.json文件。

¥To enable checkpointing by default for all sessions, you need to edit your settings.json file.

将以下键添加到您的settings.json

¥Add the following key to your settings.json:

{
  "checkpointing": {
    "enabled": true
  }
}

使用/restore命令

¥Using the /restore Command

启用后,检查点将自动创建。要管理它们,您可以使用/restore命令。

¥Once enabled, checkpoints are created automatically. To manage them, you use the /restore command.

列出可用的检查点

¥List Available Checkpoints

要查看当前项目所有已保存检查点的列表,只需运行:

¥To see a list of all saved checkpoints for the current project, simply run:

/restore

CLI 将显示可用的检查点文件列表。这些文件名通常由时间戳、正在修改的文件的名称以及即将运行的工具的名称组成(例如,2025-06-22T10-00-00_000Z-my-file.txt-write_file)。

¥The CLI will display a list of available checkpoint files. These file names are typically composed of a timestamp, the name of the file being modified, and the name of the tool that was about to be run (e.g., 2025-06-22T10-00-00_000Z-my-file.txt-write_file).

恢复特定检查点

¥Restore a Specific Checkpoint

要将项目恢复到特定检查点,请使用列表中的检查点文件:

¥To restore your project to a specific checkpoint, use the checkpoint file from the list:

/restore <checkpoint_file>

例如:

¥For example:

/restore 2025-06-22T10-00-00_000Z-my-file.txt-write_file

运行该命令后,您的文件和对话将立即恢复到创建检查点时的状态,并且将重新出现原始工具提示。

¥After running the command, your files and conversation will be immediately restored to the state they were in when the checkpoint was created, and the original tool prompt will reappear.