Reputation: 28075
Both git am
and git apply
can be used to apply patches.
It seems that git am
automatically commits, whereas git apply
only touches the files but doesn't create a commit. Is that the only difference?
Upvotes: 228
Views: 136757
Reputation: 52945
Command | Applies Patch? | Creates Commit? | Use Case |
---|---|---|---|
git apply |
Yes | No | Testing git diff patches before committing. Can also be used to apply patches to 3rd-party libraries just before building. |
git am |
Yes | Yes | Applying patches from an email or git format-patch file. Useful for collaborating and accepting someone else's contributions. |
git apply
: I think to myself: "git apply patch". It reads the supplied git diff
("patch") output and applies it to files without creating a commit. Useful for testing a patch before committing it to the repository.
Official documentation: https://git-scm.com/docs/git-apply
Reads the supplied diff output (i.e. "a patch") and applies it to files.
This command applies the patch but does not create a commit. Use
git-am
to create commits from patches generated bygit-format-patch
and/or received by email.
git am
: I think to myself: "git apply (and commit) mail". It is the reverse of git format-patch
. It takes a file created by git format-patch
, which is generally a patch emailed to you, and applies it to your repository while also creating a commit. Useful for applying patches from email or files. Can be applied to many "mailed" patches at once via git am *.patch
(one source).
Official documentation: https://git-scm.com/docs/git-am
Splits mail messages in a mailbox into commit log messages, authorship information, and patches, and applies them to the current branch. You could think of it as a reverse operation of
git-format-patch
run on a branch with a straight history without merges.
git send-email
to work with gmail to email patches to developersIn addition to the resources above, I also had some very useful and productive chats with GitHub Copilot about the topic. This answer is my own.
Upvotes: 5
Reputation: 30083
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 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).
Upvotes: 249
Reputation: 41002
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.
Upvotes: 24
Reputation: 792507
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.
Upvotes: 30