git am 和 git apply 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12240154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:30:54  来源:igfitidea点击:

What is the difference between git am and git apply?

gitpatch

提问by Christoph

Both git amand git applycan be used to apply patches. I fail to see the difference. I see a difference now: git amautomatically commits whereas git applyonly touches the files but doesn't create a commit. Is that the only difference?

双方git amgit apply可以用来应用补丁。我看不出区别。我现在看到了一个不同之处:git am自动提交而git apply只涉及文件但不创建提交。这是唯一的区别吗?

回答by georgebrock

Both the input and output are different:

输入和输出都不同:

  • git applytakes a patch (e.g. the output of git diff) and applies it to the working directory (or index, if --indexor --cachedis used).
  • git amtakes 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 apply获取补丁(例如 的输出git diff)并将其应用于工作目录(或索引,如果使用--index--cached)。
  • git am接收一个邮箱格式的提交邮件(例如 的输出git format-patch)并将它们应用到当前分支。

git amuses git applybehind the scenes, but does more work before (reading a Maildiror mbox, and parsing email messages) and after (creating commits).

git amgit apply在幕后使用,但在之前(阅读Maildirmbox,并解析电子邮件)和之后(创建提交)做了更多的工作。

回答by CB Bailey

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

git apply用于应用直接差异(例如 from git diff),而git am用于应用来自电子邮件的补丁和补丁序列,无论是 mbox 还是 Maildir 格式,并且是git format-patch. git am尝试从电子邮件消息中提取提交消息和作者详细信息,这就是它可以进行提交的原因。

回答by 0x90

With git amyou apply the patch so if you use git statusyou won't see any local changes.

随着git am应用修补程序,所以如果你使用git status,你不会看到任何地方的变化。

git applyenables you to make the changes in the source files as if you were writing the code by yourself, consequently git statusand git diffwill output the changes made in the patch you have applied, then you can fix/add more changes and submit them together as one new patch.

git apply使您可以更改在源文件中,如果你是自己编写的代码,因此git statusgit diff将输出在您已经应用,那么你就可以修复/添加更多的变化,并一起提交它们作为一个新的补丁的补丁所做的更改.