Aider 的 /undo、/diff 和 /commit 命令如何管理 AI 产生的修改
【免费下载链接】aideraider is AI pair programming in your terminal项目地址: https://gitcode.com/GitHub_Trending/ai/aider
当你让 aider 修改代码后,需要确认它改了什么、把未提交的内容纳入版本控制,以及在改动不合适时快速回退。aider 与 git 深度集成,每次 AI 编辑都会自动产生提交,并提供了/diff、/commit、/undo三个聊天内命令(in-chat commands)来完成这些操作。适用前提:aider 在一个 git 仓库目录下运行(如果目录没有 git 仓库,aider 会提示创建一个),并且已经通过/add把待编辑文件加入会话。
前提:了解 aider 自动生成提交的行为
在使用三个命令之前,先明确 aider 默认如何处理 git 历史(见 Git integration 文档):
- 每次 aider 编辑文件,都会以描述性提交信息创建一个 commit,这是
/undo和回溯审查的基础; - 如果文件已有未提交改动(dirty files),aider 会先以描述性 commit 把这些原有改动提交掉,使你的手动编辑与 aider 的编辑分开,避免 aider 的不当修改导致丢失你的工作;
- aider 创建的提交会在 git 作者/提交者名称上追加
(aider)标记,便于在历史中识别。
用 /diff 查看上一条消息以来的修改
在 aider 完成一次编辑后,输入:
/diff/diff会显示自你上一条消息以来所有文件的改动,用于在回退前确认 AI 到底改了什么。
输出判断(来自 commands.py 中的raw_cmd_diff):
- 有改动时,输出以
Diff since {sha7}...开头,后跟具体 diff 内容; - 如果上一条消息以来没有变化,aider 提示
No changes to display since the last message.,此时无需回退。
注意它对比的基准是“自上一条消息以来”,而不是“自仓库创建以来”,所以连续多轮编辑时,每轮结束后各查一次/diff是最清晰的核对方式。
用 /commit 提交工作区中其余的改动
aider 默认已经自动提交它自己的编辑,/commit主要用于把 aider 自动提交未覆盖、或在聊天之外产生的脏改动提交进仓库。命令格式(见 In-chat commands 文档):
/commit [可选的提交信息]- 不带参数时,aider 把所有脏改动连同生成的提交信息一起提交(提交信息由 weak model 根据 diff 和聊天历史生成,见 repo.py 的
get_commit_message); - 带参数时,参数内容作为提交信息直接使用;
- 如果没有脏文件,aider 提示
No more changes to commit.并结束,不会创建空提交(来自 commands.py 的raw_cmd_commit)。
用 /undo 回退最后一次 AI 提交
对不满意的 aider 修改,输入:
/undo/undo会撤销 aider 在本次会话中做的最后一次提交,并恢复该提交涉及的文件到上一个提交的状态。成功时 aider 输出两行(文档示例格式):
Removed: {sha} {commit message} Now at: {sha} {commit message}第一行是被移除的提交,第二行是当前 HEAD 所在的提交。可以用/diff再次确认工作区状态。
/undo有严格的适用条件,任一不满足都会失败并给出对应提示(实现见 commands.py 的raw_cmd_undo):
| 条件 | 不满足时的提示 |
|---|---|
| 当前目录是 git 仓库 | No git repository found. |
| 不是仓库的第一个提交 | This is the first commit in the repository. Cannot undo. |
| 最后一次提交由 aider 在本次聊天会话中做出 | The last commit was not made by aider in this chat session.,aider 会提示可尝试/git reset --hard HEAD^并明确说明这是破坏性命令 |
| 最后一次提交只有 1 个父提交(merge 提交不能撤销) | The last commit ... has more than 1 parent, can't undo. |
| 被改动文件没有新的未提交改动 | The file ... has uncommitted changes. Please stash them before undoing.,需要先 stash |
| 被改动文件在上一个提交中已存在(删除新文件的情况无法安全回退) | The file ... was not in the repository in the previous commit. Cannot undo safely. |
| 该提交尚未推送到 origin 同名分支 | The last commit has already been pushed to the origin. Undoing is not possible. |
也就是说:/undo只作用于 aider 本次会话内、未推送、非 merge 的最后一次提交。更早的改动需要借助/git命令执行原生 git 操作,或使用你习惯的 git 工具在 aider 之外管理历史。
控制这些行为的相关选项
以下选项来自 Git integration 文档,在需要调整自动提交行为时使用:
--no-auto-commits:停止 aider 为每次编辑自动创建 commit;--no-dirty-commits:停止 aider 在应用编辑前先把脏文件提交掉;--no-git:完全停止使用 git。官方不推荐此方式,启用前应自行做好文件备份;--git-commit-verify:提交时运行 pre-commit 钩子。默认 aider 使用--no-verify跳过钩子(--git-commit-verify=False);--commit-prompt:自定义生成提交信息所用的 commit prompt,可通过命令行、配置文件或环境变量设置。默认 aider 生成遵循 Conventional Commits 的提交信息;- 提交署名相关:
--no-attribute-author和--no-attribute-committer可禁用对 git 作者/提交者名称追加(aider);--attribute-co-authored-by改为在提交信息末尾追加 Co-authored-by trailer。
边界与下一步
- 以上命令都依赖 git 仓库存在且可写;在
/undo失败时,优先按上面的提示逐项核对条件,不要直接执行/git reset --hard HEAD^——它可能丢弃非 aider 产生的改动。 - aider 在聊天外也可以由你偏好的 git 工具管理历史,两个途径操作的是同一份 git 数据,修改后回到 aider 用
/diff核对即可。 - 更多聊天内命令(如
/git、/run)的完整列表见 In-chat commands 文档。
【免费下载链接】aideraider is AI pair programming in your terminal项目地址: https://gitcode.com/GitHub_Trending/ai/aider
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考