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