首页·终端命令

git apply

git

用法

git apply [--check|-3] <patch>

把一个 diff 文件打到当前工作区;它不创建提交,也不读取补丁里记的作者,所以邮件收到的补丁该用 am。

常用选项

选项含义
--checkreport whether it would apply, change nothing
-3fall back to a three-way merge and leave conflict markers
--indexapply to the working tree and stage it
--cachedapply to the index only
-Rapply in reverse, undoing the patch
-p<n>strip n leading path components
--statshow what the patch touches

示例

git apply --check fix.patch

tells you if it fits before you try

git apply -3 fix.patch

applies it and leaves conflicts to resolve by hand

git apply -R fix.patch

takes an applied patch back out

难的地方在于撤销有好几种:把分支的指向挪走,和补一个反向提交,不是一回事。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. git apply 是做什么的?

把一个 diff 文件打到当前工作区;它不创建提交,也不读取补丁里记的作者,所以邮件收到的补丁该用 am。

Q. 怎么写?

git apply [--check|-3] <patch> —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 7 个,完整列表在 man git。这条命令属于git。

相关命令

man 手册: man git-apply