补丁如何在 Git 中工作?

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

How do patches work in Git?

gitpatch

提问by Pieter

I'm new to Git, but familiar with SVN. As a test I made a repository in a local directory with git init. Then I cloned the empty repository (over SSH using 127.0.0.1, which is another thing I wanted to test) to another local directory. I added some files in repository 2, I did git add *and finally git commit -a -m "First source code".

我是 Git 的新手,但熟悉 SVN。作为测试,我在本地目录中创建了一个存储库git init。然后我将空存储库(通过 SSH 使用 127.0.0.1,这是我想测试的另一件事)克隆到另一个本地目录。我在存储库 2 中添加了一些文件,我做了git add *,最后git commit -a -m "First source code".

I now want to create a patch using git format-patchand apply it on repository 1. How do I do this? I know there's a manual, but these things are terribly complicated and make me wanna do certain things to my monitor.

我现在想创建一个补丁git format-patch并将其应用到存储库 1 上。我该怎么做?我知道有手册,但这些东西非常复杂,让我想对我的显示器做某些事情。

采纳答案by Jon

Create your patch via:

通过以下方式创建您的补丁:

$ git format-patch master --stdout > patch.diff

then patch.diff will contain the diff, which you can then send to someone else to apply using:

然后 patch.diff 将包含差异,然后您可以将其发送给其他人以使用:

$ git am < patch.diff

Sometimes, when the manuals are a little dense, it makes sense to look for a tutorial:

有时,当手册有点密集时,寻找教程是有意义的:

http://luhman.org/blog/2009/09/22/git-patch-tutorial

http://luhman.org/blog/2009/09/22/git-patch-tutorial

回答by Jakob Borg

The easiest method to create patches from the last commit (or last few commits) is to use format-patchwith a negative number indicating the number of commits to create patches for:

从最后一次提交(或最后几次提交)创建补丁的最简单方法是使用format-patch负数表示为以下内容创建补丁的提交次数:

git format-patch -1

You'll get a patch file named after the commit description. The use amto insert it into another repository:

您将获得一个以提交描述命名的补丁文件。am将其插入另一个存储库的用途:

git am << name_of_patch_file

回答by Steven Penny

Using GitHub patch

使用 GitHub 补丁

  1. Add .patchto a commit URL to get the patch file, example

    github.com/git/git/commit/b6b3b6a.patch

  2. Patch the original file like this:

    git am /tmp/b6b3b6a.patch
    
  1. 添加.patch到提交 URL 以获取补丁文件,示例

    github.com/git/git/commit/b6b3b6a.patch

  2. 像这样修补原始文件:

    git am /tmp/b6b3b6a.patch
    

Using GitHub diff

使用 GitHub 差异

  1. Add .diffto a commit URL to get the patch file, example

    github.com/git/git/commit/b6b3b6a.diff

  2. Patch the original file like this:

    git apply -p0 /tmp/b6b3b6a.diff
    
  1. 添加.diff到提交 URL 以获取补丁文件,示例

    github.com/git/git/commit/b6b3b6a.diff

  2. 像这样修补原始文件:

    git apply -p0 /tmp/b6b3b6a.diff
    

§5.3 Distributed Git - Maintaining a Project

§5.3 分布式 Git - 维护一个项目

回答by Ana Betts

The proper and easier way to do this if you're using Git is via remotes:

如果您使用 Git,正确且更简单的方法是通过遥控器:

cd \path\to\repo1
git remote add otherrepo \path\to\repo2
git fetch otherrepo

git log otherrepo/master  ## Find the commit you want to steal in the list

git cherry-pick SOME_SHA1  ## Snag just one commit
git merge otherrepo/master  ## Merge all of the new commits from otherrepo/master

This will migrate commitsfrom one repo to another, including their authors and commit messages, and will help you sort out merge conflicts (especially if you're moving > 1 commit)

这会将提交从一个 repo迁移到另一个 repo,包括他们的作者和提交消息,并将帮助您解决合并冲突(尤其是当您移动 > 1 个提交时)

回答by VonC

With Git 2.25 (Q1 2020), git format-patchevolves to better use the branch description ("git branch --edit-description") as subject.

使用 Git 2.25(2020 年第一季度),git format-patch可以更好地使用分支描述(“ git branch --edit-description”)作为主题。

See commit bf8e65b, commit a92331d, commit 46273df(15 Oct 2019) by Denton Liu (Denton-L).
(Merged by Junio C Hamano -- gitster--in commit b75ba9b, 10 Nov 2019)

请参阅提交 bf8e65b提交 a92331d提交 46273df(2019 年 10 月 15 日)由Denton Liu ( Denton-L)
(由Junio C gitsterHamano合并-- --提交 b75ba9b 中,2019 年 11 月 10 日)

format-patch: teach --cover-from-descriptionoption

Signed-off-by: Denton Liu

Before, when format-patchgenerated a cover letter, only the body would be populated with a branch's description while the subject would be populated with placeholder text.

However, users may want to have the subject of their cover letter automatically populated in the same way.

Teach format-patch to accept the --cover-from-descriptionoption and corresponding format.coverFromDescriptionconfig, allowing users to populate different parts of the cover letter (including the subject now).

format-patch: 教学--cover-from-description选项

签字人:Denton Liu

以前,当format-patch生成求职信时,只有正文会填充分支的描述,而主题会填充占位符文本。

但是,用户可能希望以相同的方式自动填充求职信的主题。

教 format-patch 接受--cover-from-description选项和相应的format.coverFromDescription配置,允许用户填充求职信的不同部分(包括现在的主题)。

The git configdocumentationnow includes:

git config文档现在包括:

format.coverFromDescription:

The default mode for format-patch to determine which parts of the cover letter will be populated using the branch's description.

format.coverFromDescription:

format-patch 的默认模式,用于确定将使用分支描述填充求职信的哪些部分。

And git format-patch:

并且git format-patch

--cover-from-description=<mode>:

Controls which parts of the cover letter will be automatically populated using the branch's description.

  • If <mode>is messageor default, the cover letter subject will be populated with placeholder text.
    The body of the cover letter will be populated with the branch's description. This is the default mode when no configuration nor command line option is specified.

  • If <mode>is subject, the first paragraph of the branch description will populate the cover letter subject.
    The remainder of the description will populate the body of the cover letter.

  • If <mode>is auto, if the first paragraph of the branch description is greater than 100 bytes, then the mode will be message, otherwise subjectwill be used.

  • If <mode>is none, both the cover letter subject and body will be populated with placeholder text.

--cover-from-description=<mode>:

控制将使用分支机构的描述自动填充求职信的哪些部分。

  • 如果<mode>messagedefault,则求职信主题将填充占位符文本。
    求职信的正文将填充分支的描述。这是未指定配置或命令行选项时的默认模式。

  • 如果<mode>subject,分支描述的第一段将填充求职信主题。
    描述的其余部分将填充求职信的正文。

  • 如果<mode>auto,如果分支描述的第一段大于100字节,则模式为message,否则subject使用。

  • 如果<mode>none,则求职信主题和正文都将填充占位符文本。

回答by baraber

You have to go to "repository 2", the one you want to create the patch from, and run git-format-patch to create the patch : git format-patch master --stdout > name_of_patch_file

您必须转到要从中创建补丁的“存储库 2”,然后运行 ​​git-format-patch 来创建补丁: git format-patch master --stdout > name_of_patch_file

Then you go in "repository 1", the one you want to apply the patch to : git apply name_of_patch_file

然后你进入“repository 1”,你想应用补丁的那个: git apply name_of_patch_file

Sometimes it is useful to just check if the patch will cause problems : git apply --check name_of_patch_file

有时检查补丁是否会导致问题很有用: git apply --check name_of_patch_file