Home·Terminal commands

git apply

git

Usage

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

Applies a diff file to your working tree; it makes no commit and ignores the author recorded in the patch, so mailed patches belong to am instead.

Common flags

FlagMeaning
--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

Examples

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

The confusion is that undo has several shapes — moving where a branch points is not the same as adding a commit that reverses it.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does git apply do?

Applies a diff file to your working tree; it makes no commit and ignores the author recorded in the patch, so mailed patches belong to am instead.

Q. How do I type it?

git apply [--check|-3] <patch> — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

7 are listed here; the full set is in man git. This command sits under git.

Related commands

man page: man git-apply