首頁·終端機命令

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