如何为特定提交生成 git 补丁?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6658313/
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 11:20:40  来源:igfitidea点击:

How to generate a git patch for a specific commit?

gitpatch

提问by elle

I need to write a script that creates patches for a list of SHA1 commit numbers.

我需要编写一个脚本来为 SHA1 提交编号列表创建补丁。

I tried using git format-patch <the SHA1>, but that generated a patch for each commit since that SHA1. After a few hundred patches were generated, I had to kill the process.

我尝试使用git format-patch <the SHA1>,但是自从那个 SHA1 之后,每次提交都会生成一个补丁。在生成了几百个补丁后,我不得不终止该进程。

Is there a way to generate a patch only for the specific SHA1?

有没有办法只为特定的 SHA1 生成补丁?

回答by manojlds

Try:

尝试:

git format-patch -1 <sha>

or

或者

git format-patch -1 HEAD

According to the documentation link above, the -1flag tells git how many commits should be included in the patch;

根据上面的文档链接,该-1标志告诉 git 补丁中应该包含多少提交;

-<n>

     Prepare patches from the topmost commits.

-<n>

     从最顶层的提交准备补丁。



Apply the patch with the command:

使用以下命令应用补丁:

git am < file.patch

回答by Sriram Murali

For generating the patches from the topmost commits from a specific sha1 hash:

为了从特定 sha1 哈希的最顶层提交生成补丁:

git format-patch -<n> <SHA1>

The last 10 patches from head in a single patch file:

单个补丁文件中 head 的最后 10 个补丁:

git format-patch -10 HEAD --stdout > 0001-last-10-commits.patch

回答by brandon

Say you have commit id 2 after commit 1 you would be able to run:

假设您在提交 1 之后提交了 id 2,您将能够运行:

git diff 2 1 > mypatch.diff

where 2 and 1 are SHA hashes.

其中 2 和 1 是 SHA 哈希。

回答by kenorb

This command (as suggested already by @Naftuli Tzvi Kay):

这个命令(正如@ Naftuli Tzvi Kay已经建议的那样):

git format-patch -1 HEAD

Replace HEADwith specific hash or range.

替换HEAD为特定的哈希或范围。

will generate the patch file for the latest commit formatted to resemble UNIX mailbox format.

将为最新提交生成补丁文件,格式类似于 UNIX 邮箱格式。

-<n>- Prepare patches from the topmost commits.

-<n>- 从最顶层的提交准备补丁。

Then you can re-apply the patch file in a mailbox format by:

然后您可以通过以下方式以邮箱格式重新应用补丁文件:

git am -3k 001*.patch

See: man git-format-patch.

见:man git-format-patch

回答by zdrsoft

git format-patch commit_Id~1..commit_Id  
git apply patch-file-name

Fast and simple solution.

快速简单的解决方案。

回答by VonC

If you want to be sure the (single commit) patch will be applied on top of a specific commit, you can use the new git 2.9 (June 2016) option git format-patch --base

如果您想确保(单次提交)补丁将应用于特定提交之上,您可以使用新的 git 2.9(2016 年 6 月)选项 git format-patch --base

git format-patch --base=COMMIT_VALUE~ -M -C COMMIT_VALUE~..COMMIT_VALUE

# or
git format-patch --base=auto -M -C COMMIT_VALUE~..COMMIT_VALUE

# or
git config format.useAutoBase true
git format-patch -M -C COMMIT_VALUE~..COMMIT_VALUE

See commit bb52995, commit 3de6651, commit fa2ab86, commit ded2c09(26 Apr 2016) by Xiaolong Ye (``).
(Merged by Junio C Hamano -- gitster--in commit 72ce3ff, 23 May 2016)

参见Xiaolong Ye (``) 的提交 bb52995提交 3de6651提交 fa2ab86提交 ded2c09(2016 年 4 月 26 日
(由Junio C gitsterHamano合并-- --commit 72ce3ff,2016 年 5 月 23 日)

format-patch: add '--base' option to record base tree info

Maintainers or third party testers may want to know the exact base tree the patch series applies to. Teach git format-patch a '--base' option to record the base tree info and append it at the end of the first message (either the cover letter or the first patch in the series).

The base tree info consists of the "base commit", which is a well-known commit that is part of the stable part of the project history everybody else works off of, and zero or more "prerequisite patches", which are well-known patches in flight that is not yet part of the "base commit" that need to be applied on top of "base commit" in topological order before the patches can be applied.

The "base commit" is shown as "base-commit:" followed by the 40-hex of the commit object name.
A "prerequisite patch" is shown as "prerequisite-patch-id:" followed by the 40-hex "patch id", which can be obtained by passing the patch through the "git patch-id --stable" command.

format-patch: 添加 ' --base' 选项以记录基树信息

维护人员或第三方测试人员可能想知道补丁系列适用的确切基础树。教 git format-patch 一个 ' --base' 选项来记录基本树信息并将其附加到第一条消息的末尾(求职信或系列中的第一个补丁)。

基础树信息包括“基础提交”,这是一个众所周知的提交,是项目历史稳定部分的一部分,其他人都在工作,以及零个或多个“先决条件补丁”,这是众所周知的在应用补丁之前,尚不属于“基本提交”的正在运行的补丁需要按拓扑顺序应用在“基本提交”之上。

“基本提交”显示为“ base-commit:”,后跟提交对象名称的 40 进制。
“先决条件补丁”显示为“ prerequisite-patch-id:”后跟 40 进制的“补丁 ID”,可以通过“ git patch-id --stable”命令传递补丁获得。



Git 2.23 (Q3 2019) will improve that, because the "--base" option of "format-patch" computed the patch-idsfor prerequisite patches in an unstable way, which has been updated to compute in a way that is compatible with "git patch-id --stable".

Git 2.23(2019 年第 3 季度)将改进这一点,因为“ --base”的“ ”选项以不稳定的方式format-patch计算patch-ids先决条件补丁,该方法已更新为以与“ git patch-id --stable”兼容的方式计算。

See commit a8f6855, commit 6f93d26(26 Apr 2019) by Stephen Boyd (akshayka).
(Merged by Junio C Hamano -- gitster--in commit 8202d12, 13 Jun 2019)

请参阅提交 a8f6855提交 6f93d26(2019 年 4 月 26 日)由Stephen Boyd ( akshayka)提交
(由Junio C gitsterHamano合并-- --提交 8202d12 中,2019 年 6 月 13 日)

format-patch: make --base patch-idoutput stable

We weren't flushing the context each time we processed a hunk in the patch-idgeneration code in diff.c, but we were doing that when we generated "stable" patch-ids with the 'patch-id' tool.

Let's port that similar logic over from patch-id.cinto diff.cso we can get the same hash when we're generating patch-ids for 'format-patch --base=' types of command invocations.

format-patch: 使--base patch-id输出稳定

每次在 中处理patch-id生成代码中的一个大块时,我们并没有刷新上下文 diff.c,但是当我们使用“ patch-id”工具生成“稳定”补丁 ID 时,我们正在这样做。

让我们将类似的逻辑从patch-id.cinto移植过来,diff.c这样当我们为“ format-patch --base=”类型的命令调用生成补丁 ID 时,我们可以获得相同的哈希值。



Before Git 2.24 (Q4 2019), "git format-patch -o <outdir>" did an equivalent of "mkdir <outdir>" not "mkdir -p <outdir>", which is being corrected.

在 Git 2.24(2019 年第 4 季度)之前,“ git format-patch -o <outdir>” 相当于“ mkdir <outdir>” not “ mkdir -p <outdir>”,正在纠正中。

See commit edefc31(11 Oct 2019) by Bert Wesarg (bertwesarg).
(Merged by Junio C Hamano -- gitster--in commit f1afbb0, 18 Oct 2019)

请参阅Bert Wesarg ( ) 的commit edefc31(2019 年 10 月 11 日(由Junio C Hamano合并-- --提交 f1afbb0 中,2019 年 10 月 18 日)bertwesarg
gitster

format-patch: create leading components of output directory

Signed-off-by: Bert Wesarg

'git format-patch -o ' did an equivalent of 'mkdir ' not 'mkdir -p ', which is being corrected.

Avoid the usage of 'adjust_shared_perm' on the leading directories which may have security implications. Achieved by temporarily disabling of 'config.sharedRepository' like 'git init' does.

format-patch: 创建输出目录的主要组件

签字人:Bert Wesarg

'git format-patch -o ' 相当于 'mkdir ' 而不是 'mkdir -p ',正在纠正。

避免在adjust_shared_perm可能具有安全隐患的前导目录上使用“ ”。通过config.sharedRepository像“ git init”那样暂时禁用“ ”来实现。



With Git 2.25 (Q1 2020), "git rebase" did not work well when format.useAutoBaseconfiguration variable is set, which has been corrected.

在 Git 2.25(2020 年第 1 季度)中,设置配置变量git rebase时“ ”无法正常工作format.useAutoBase,这已得到纠正。

See commit cae0bc0, commit 945dc55, commit 700e006, commit a749d01, commit 0c47e06(04 Dec 2019) by Denton Liu (Denton-L).
(Merged by Junio C Hamano -- gitster--in commit 71a7de7, 16 Dec 2019)

请参阅提交 cae0bc0提交 945dc55提交 700e006提交 a749d01提交 0c47e06(2019 年 12 月 4 日)由Denton Liu ( Denton-L)
(由Junio C gitsterHamano合并-- --commit 71a7de7,2019 年 12 月 16 日)

rebase: fix format.useAutoBasebreakage

Reported-by: Christian Biesinger
Signed-off-by: Denton Liu

With format.useAutoBase = true, running rebase resulted in an error:

fatal: failed to get upstream, if you want to record base commit automatically,
please use git branch --set-upstream-to to track a remote branch.
Or you could specify base commit by --base=<base-commit-id> manually
error:
git encountered an error while preparing the patches to replay
these revisions:

ede2467cdedc63784887b587a61c36b7850ebfac..d8f581194799ae29bf5fa72a98cbae98a1198b12

As a result, git cannot rebase them.

Fix this by always passing --no-baseto format-patch from rebase so that the effect of format.useAutoBaseis negated.

rebase: 修复format.useAutoBase破损

报告人:Christian Biesinger
签字人:Denton Liu

使用format.useAutoBase = true,运行 rebase 会导致错误:

fatal: failed to get upstream, if you want to record base commit automatically,
please use git branch --set-upstream-to to track a remote branch.
Or you could specify base commit by --base=<base-commit-id> manually
error:
git encountered an error while preparing the patches to replay
these revisions:

ede2467cdedc63784887b587a61c36b7850ebfac..d8f581194799ae29bf5fa72a98cbae98a1198b12

As a result, git cannot rebase them.

通过始终--no-base从 rebase传递给 format-patch 来解决此问题,从而消除 的影响format.useAutoBase

回答by Makah

To generate path from a specific commit (not the last commit):

从特定提交(不是最后一次提交)生成路径:

git format-patch -M -C COMMIT_VALUE~1..COMMIT_VALUE

回答by jiahut

if you just want diff the specified file, you can :

如果您只想比较指定的文件,您可以:

git diff master 766eceb -- connections/ > 000-mysql-connector.patch

git diff master 766eceb --connections/> 000-mysql-connector.patch

回答by alls0rts

With my mercurial background I was going to use:

凭借我的善变背景,我将使用:

git log --patch -1 $ID > $file

But I am considering using git format-patch -1 $IDnow.

但我现在正在考虑使用git format-patch -1 $ID

回答by Ankush

What is the way to generate a patch only for the specific SHA1?

仅针对特定 SHA1 生成补丁的方法是什么?

It's quite simple:

这很简单:

Option 1. git show commitID > myFile.patch

选项1。 git show commitID > myFile.patch

Option 2. git commitID~1..commitID > myFile.patch

选项 2。 git commitID~1..commitID > myFile.patch

Note: Replace commitIDwith actual commit id (SHA1 commit code).

注意:替换commitID为实际提交 ID(SHA1 提交代码)。