What is the difference between git am and git apply?
Asked Answered
O

4

212

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?

Orth answered 2/9, 2012 at 22:10 Comment(1)
am could be thought of as an abbreviation of Apply Mail...Coquette
R
237

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).

Ribbonwood answered 2/9, 2012 at 22:23 Comment(4)
Note: 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
Which of these would I normally want if I used 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.eeAcademy
looks like git apply is the command to do, in the situation I described above. I would assume the same applies if I create the patch from a sequence of commands in GITK? but I don't need to know right now, so I'm not going to try it and see. Feel free to do so yourself!Academy
N
28

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.

Narcho answered 2/9, 2012 at 22:21 Comment(0)
L
23

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.

Lair answered 2/9, 2012 at 22:20 Comment(0)
F
1

Quick summary

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.

Details

  1. 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 by git-format-patch and/or received by email.

  2. 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.

See also

  1. How to apply a patch generated with git format-patch?
  2. My Q&A: How to configure and use git send-email to work with gmail to email patches to developers

In 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.

Florrieflorry answered 4/12, 2023 at 16:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.