“git format-patch”和“git diff”有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4624127/
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
What is the difference between 'git format-patch and 'git diff'?
提问by Rafid
I don't see a difference between the output of 'git format-patch' and 'git diff', is there any? And won't I be able to use 'git diff' to produce a patch and then apply it using git apply?
我看不出 'git format-patch' 和 'git diff' 的输出有什么区别,有什么区别吗?我不能使用“git diff”来生成补丁,然后使用 git apply 应用它吗?
My problem is that I have changes added to the index, but apparently git format-patch only accepts commits, so if I can use the output of diff, then I can use this command to produce a patch for the changes in the index:
我的问题是我在索引中添加了更改,但显然 git format-patch 只接受提交,所以如果我可以使用 diff 的输出,那么我可以使用此命令为索引中的更改生成补丁:
git diff --cached > index.patch
回答by Sylvain Defresne
A patch created with git format-patch
will also include some meta-information about the commit (committer, date, commit message, ...) and will contains diff of binary data. Everything will be formatted as a mail, so that it can be easily sent. The person that receive it can then recreate the corresponding commit with git am
and all meta-data will be intact. It can also be applied with git apply
as it is a super-set of a simple diff.
创建的补丁git format-patch
还将包含有关提交的一些元信息(提交者、日期、提交消息等),并将包含二进制数据的差异。一切都将格式化为邮件,以便可以轻松发送。收到它的人然后可以重新创建相应的提交,git am
并且所有元数据都将完好无损。它也可以应用,git apply
因为它是简单差异的超集。
A patch crated with git diff
will be a simple diff with context (think diff -u
). It can also be applied with git apply
but the meta-data will not be recreated (as they are not present).
打包的补丁git diff
将是一个简单的上下文差异(想想diff -u
)。它也可以应用,git apply
但不会重新创建元数据(因为它们不存在)。
In summary, git format-patch
is useful to transmit a commit, while git diff
is useful to get a diff between two trees.
总之,git format-patch
对于传输提交git diff
很有用,而对于获取两棵树之间的差异很有用。
回答by Abizern
From the manuals git-format-patchprepares patches suitable for email submission, while git-diffshows changes.
从手册中git-format-patch准备适合电子邮件提交的补丁,而git-diff显示更改。
They are two different things and have different purposes, they just happen to output a patch format. But git-format-patch
adds data about a commit (date, author, commit message) and bundles it up into a format that is suitable for sending as a Unix mail message (although these are just files, so they can be sent to other methods and still applied by git-am).
它们是两种不同的东西,具有不同的用途,它们只是碰巧输出一种补丁格式。但是git-format-patch
添加有关提交的数据(日期,作者,提交消息)并将其捆绑成适合作为 Unix 邮件消息发送的格式(尽管这些只是文件,因此它们可以发送到其他方法并仍然由git-am)。
Also git-format-patch
generates a patch file for each commit in the range that you specify. These changes will be added as commits to your repository with git-am
.
还会git-format-patch
为您指定范围内的每个提交生成一个补丁文件。这些更改将作为提交添加到您的存储库中git-am
。
git-diff
just shows the diff between the two states you ask for, and can be used to create a patch file. But this is just a normal patch file and applying the patch will just change the state of the working directory.
git-diff
仅显示您要求的两种状态之间的差异,并可用于创建补丁文件。但这只是一个普通的补丁文件,应用补丁只会改变工作目录的状态。
And yes, you can create a patch for your index that way.
是的,您可以通过这种方式为索引创建补丁。
回答by K. Symbol
The patch file can be generated with the git diff
command, but comparing with the patch generated by the git format-patch
command, the major differences are:
可以使用git diff
命令生成补丁文件,但与git format-patch
命令生成的补丁相比,主要区别在于:
- No metadata about a commit (such as date, author, commit message, etc.) ;
- No statistics about the diff (diffstat, such as x files changed, y insertions(+), z deletions(-));
- No binary diffs, only textual diffs.
- 没有关于提交的元数据(例如日期、作者、提交消息等);
- 没有关于diff的统计(diffstat,比如x个文件改变了,y个插入(+),z个删除(-));
- 没有二进制差异,只有文本差异。
To generate the patch file for all changed files (in the index or the working directory):
要为所有更改的文件(在索引或工作目录中)生成补丁文件:
git diff HEAD --binary > my.patch
# git diff + git diff --cached/staged == git diff HEAD
To apply the generated patch file:
应用生成的补丁文件:
# restore the changed files firstly
git restore --staged .
git restore .
# apply the patch to redo the changes
git apply my.patch
# or
patch -p1 < my.patch