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?
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).
git apply
seems to also accept git format-patch
output. –
Interstitial git apply
would work for output from git format-patch
as well but the changes would be unstaged and would need to be committed (thus creating a different commit point in the index they are applied to). With git am
you would be carrying the commit information (along with author, etc.) into the index it is applied to. git apply
then is for patching your repo (bad), git am
can take legit feature changes and include it into your repo (preferred approach). –
Lecialecithin GITK
to view my tree and then made a patch from the context menu on my uncommitted changes? I can't imagine git am would work very well if there's no commit information for it to create the commits, so I would guess git apply is what I want.… <br> …I guess, both in the name of general prudence and because of the moderation strike (My trust for this site is running a little low right now, if they don't understand the problem with AI answers), I should tryitands.ee –
Academy 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.
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.
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.
© 2022 - 2024 — McMap. All rights reserved.
am
could be thought of as an abbreviation ofApply Mail
... – Coquette