简体   繁体   English

git am 和 git apply 有什么区别?

[英]What is the difference between git am and git apply?

Both git am and git apply can be used to apply patches. git amgit apply均可用于应用补丁。 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.我现在看到了不同之处: git am自动提交而git apply只涉及文件但不创建提交。 Is that the only difference?这是唯一的区别吗?

Both the input and output are different:输入和输出都不同:

  • git apply takes a patch (eg the output of git diff ) and applies it to the working directory (or index, if --index or --cached is used). git apply获取一个补丁(例如git diff的输出)并将其应用到工作目录(或索引,如果使用--index--cached )。
  • git am takes a mailbox of commits formatted as an email messages (eg the output of git format-patch ) and applies them to the current branch. git am接收一个邮箱格式的提交邮件(例如git format-patch的输出)并将它们应用到当前分支。

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 am在幕后使用git apply ,但在之前(读取Maildirmbox ,以及解析电子邮件)和之后(创建提交)做了更多工作。

git apply is for applying straight diffs (eg 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 apply用于应用直接差异(例如从git diff ),而git am用于应用来自电子邮件的补丁和补丁序列,无论是 mbox 还是 Maildir 格式,并且是git format-patch的“相反”。 git am tries to extract commit messages and author details from email messages which is why it can make commits. git am尝试从电子邮件消息中提取提交消息和作者详细信息,这就是它可以进行提交的原因。

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.使用git am你应用补丁,所以当你运行git status你不会看到任何本地更改,但是git log会显示补丁已经提交到源代码。

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.但是使用git apply您可以在源文件中进行更改,就像您自己编写代码一样,因此git statusgit diff将输出您应用的补丁中出现的更改。 Hence with git apply you can fix/add more changes and git add them together as a single new patch.因此,使用git apply您可以修复/添加更多更改git add它们作为单个新补丁git add在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM