apply-patch
它能做什么
这项技能使用 Git 把一组代码改动作为一个补丁应用。适合需要修改多个文件的智能体,或者简单编辑总是匹配失败的情况。
适用场景
- ✓当一项改动需要编辑三个或更多文件时。
- ✓当简单的查找替换编辑因文本不匹配而不断失败的时候。
- ✓当你想应用一个来自拉取请求或 git diff 的补丁时。
技能链接
https://github.com/agentscope-ai/agentscope-java/blob/main/agentscope-examples/agents/agentscope-codingagent/src/main/resources/workspace-templates/skills/apply-patch/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请把这个模块中四个文件的同一个重命名作为一个补丁应用,并先检查它能否干净地应用。”
每周 5 个新技能,1 分钟读完。
工作原理
这项技能会写出描述改动的统一差异格式(unified diff),检查它能否应用,然后用 Git 一次性应用。如果补丁被拒绝,智能体会重新读取当前的行,并重新生成补丁。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Apply Patch
The built-in edit_file does an exact string match on a single file and has no fuzzy
tolerance. For multi-file changes, or when edit_file keeps failing to find the string,
construct a unified diff and apply it atomically with git.
Procedure
Write the unified diff to a file in the sandbox (use
write_fileor a heredoc viaexecute):diff --git a/path/to/file b/path/to/file --- a/path/to/file +++ b/path/to/file @@ -10,7 +10,7 @@ context line -old line +new lineValidate before applying:
git apply --check changes.patchApply:
git apply changes.patch(use-p0if paths are nota/...b/...).If
git applyrejects the hunk, the surrounding context is stale —read_filethe exact lines again, regenerate the diff with correct context, and retry.
When to prefer this
- Editing 3+ files for one logical change (atomic, all-or-nothing).
- Repetitive or whitespace-sensitive edits where
edit_fileexact-match struggles. - Re-applying a patch produced elsewhere (e.g. from a PR or
git diff).
Always run the verify-changes skill afterwards.
常见问题
它会先检查补丁吗?
会。技能会在应用补丁之前进行检查。
如果补丁失败会怎样?
智能体会重新读取文件的当前内容,并生成新的补丁。
这是全部成功或全部失败吗?
是的。补丁作为一次改动应用,因此设计上是全部成功或全部失败。
详情
作者的原始描述
Apply multi-file or tricky edits atomically with git apply instead of many fragile edit_file calls. Use when changing several files at once or when edit_file fails to match.
热度
- GitHub 星标
- 6.2k
- 排名
- #14386
- 许可证
- Apache-2.0
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
agentscope-java
6.2k帮你的智能体用 Java 编写非阻塞的 AgentScope 代码,AgentScope 是基于 Project Reactor 的多智能体框架。
data-report
6.2k帮你的智能体把数据集汇总成报告,包括平均值、总数、范围和趋势,并保存为 Markdown 文件。
data-transform
6.2k帮你的智能体在 CSV 和 JSON 等格式之间转换数据文件,使数据可以在其他工具中使用。
git-changelog
6.2k帮你的智能体根据 Git 历史编写变更日志或发布说明,并按新功能、修复和其他改动分组。