What is the difference between git am and git apply?

GitPatch

Git Problem Overview


Both git am and git apply can be used to apply patches. I fail to see the difference. I see a difference now: git am automatically commits whereas git apply only touches the files but doesn't create a commit. Is that the only difference?

Git Solutions


Solution 1 - Git

Both the input and output are different:

  • git apply takes a patch (e.g. the output of git diff) and applies it to the working directory (or index, if --index or --cached is used).
  • git am takes a mailbox of commits formatted as an email messages (e.g. the output of git format-patch) and applies them to the current branch.

git am uses git apply behind the scenes, but does more work before (reading a Maildir or mbox, and parsing email messages) and after (creating commits).

Solution 2 - Git

git apply is for applying straight diffs (e.g. from git diff) whereas git am is for applying patches and sequences of patches from emails, either mbox or Maildir format and is the "opposite" of git format-patch. git am tries to extract commit messages and author details from email messages which is why it can make commits.

Solution 3 - Git

With git am you apply the patch so when you run git status you won't see any local changes, but git log will show the patch have been committed to the source code.

But with git apply you make the changes in the source files as if you were writing the code yourself, consequently git status and git diff will output the changes appeared in the patch you applied. Hence with git apply you can fix/add more changes and git add them together as a single new patch.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChristophView Question on Stackoverflow
Solution 1 - GitgeorgebrockView Answer on Stackoverflow
Solution 2 - GitCB BaileyView Answer on Stackoverflow
Solution 3 - Git0x90View Answer on Stackoverflow