什么是 git 版本控制中的补丁?

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

What is a patch in git version control?

gitversion-control

提问by Amit Erandole

I am new to both git and version control so I am trying to figure out what a patch is and how is it different from the rest of activities I do in git?

我是 git 和版本控制的新手,所以我想弄清楚补丁是什么以及它与我在 git 中所做的其他活动有什么不同?

When do I apply a patch? Does it happen every time I commit?

我什么时候应用补丁?每次我提交时都会发生吗?

采纳答案by VonC

You can see in this blog posthow you can create a patch (collection of changes you want to communicate and apply to another repo)

您可以在此博客文章中看到如何创建补丁(您想要传达并应用于另一个存储库的更改集合)

git patch
(picture from the 2008 blog post "Bioruby with git: how would that work?", published by Jan AERTS)

补丁
(图片来自 2008 年的博客文章“ Bioruby with git: how would that work?”,由Jan AERTS发布)

See also Contributing to Rails with Gitas another concrete example.

另请参阅使用 GitRails 做出贡献作为另一个具体示例。

Nowadays, the GitHub pull request makes it really easy to apply patcheson GitHub repos, which is useful when you aren't a direct contributor (ie you have no right to directly push to a repo).
Actually, fairly recently GitHub introduced "Better Pull Request Emails" to improve the notification of new patches.

如今,GitHub 拉取请求使得在 GitHub 存储库上应用补丁变得非常容易,这在您不是直接贡献者(即您无权直接推送到存储库)时非常有用。
实际上,最近 GitHub 推出了“ Better Pull Request Emails”来改进新补丁的通知。

回答by VonC

Patchis a Unix program that updates text files according to instructions contained in a separate file, called a patch file.

Patch是一个 Unix 程序,它根据一个单独的文件(称为补丁文件)中包含的指令更新文本文件。

So, in other words it may mean the file with instructions or a program that processes that file and applies it to something.

因此,换句话说,它可能意味着包含指令的文件或处理该文件并将其应用于某些内容的程序。

Now, what is a patch file? Let's say you have a text file with 2 lines:

现在,什么是补丁文件?假设您有一个包含 2 行的文本文件:

This is line A.
This is line B, or otherwise #2.

Then you change the first line, and now your file looks like this:

然后您更改第一行,现在您的文件如下所示:

This is SPARTA.
This is line B, or otherwise #2.

How would you describe the change to the contents of the file? You can say that first line "This is line A." got replaced with "This is SPARTA.", or even the last word "A" of the first line replaced with another word "SPARTA". And that is exactly what difftells us. Let's say I have two versions of this file, one called file1.txt and another one file2.txt, then I run diff and get this:

您如何描述对文件内容的更改?你可以说第一行“这是 A 行”。被替换为“This is SPARTA.”,甚至第一行的最后一个单词“A”替换为另一个单词“SPARTA”。这正是diff告诉我们的。假设我有这个文件的两个版本,一个叫做 file1.txt,另一个叫做 file2.txt,然后我运行 diff 并得到这个:

$ diff -u file1.txt file2.txt 
--- file1.txt   2011-11-26 11:07:03.131010360 -0500
+++ file2.txt   2011-11-26 11:07:13.171010362 -0500
@@ -1,2 +1,2 @@
-This is line A.
+This is SPARTA.
 This is line B, or otherwise #2.

Having a description of changes, you can apply it to a initial content and get a modified content. And those changes, put in unified format that "patch"-like programs can understand, is called a patch file. It's like instead of getting a fish from someone they teach you how to fish, so that you can dig that fish out of the waters yourself. Now, let's apply our patch to file1.txt to make it look exactly like file2.txt:

有了更改的描述,您可以将其应用于初始内容并获得修改后的内容。而这些变化,以类似“补丁”的程序可以理解的统一格式,被称为补丁文件。这就像他们教你如何钓鱼,而不是从别人那里得到一条鱼,这样你就可以自己把那条鱼从水里挖出来。现在,让我们将补丁应用到 f​​ile1.txt 使其看起来与 file2.txt 完全一样:

$ cat file1.txt 
This is line A.
This is line B, or otherwise #2.
$ cat file2.txt 
This is SPARTA.
This is line B, or otherwise #2.
$ diff -u file1.txt file2.txt > changes.patch
$ cat changes.patch 
--- file1.txt   2011-11-26 11:09:38.651010370 -0500
+++ file2.txt   2011-11-26 11:07:13.171010362 -0500
@@ -1,2 +1,2 @@
-This is line A.
+This is SPARTA.
 This is line B, or otherwise #2.
$ patch < changes.patch 
patching file file1.txt
$ cat file1.txt 
This is SPARTA.
This is line B, or otherwise #2.
$ 

You may think that it is easier to just have two versions of this file. Well, in this simple case that is true. But when you have a lot of files, and those files are very big, it is a lot more efficient to have a few lines of changes rather than two copies of the whole thing.

您可能认为只有此文件的两个版本更容易。嗯,在这个简单的情况下,这是真的。但是当你有很多文件,而且这些文件非常大时,有几行更改而不是整个文件的两个副本会更有效率。

When talking in terms of git, patch file still means the same thing, but using diff + patch yourself would be a nightmare. For example, you will always have to have two versions of the file (or even the whole repository) checked out in order to compare them. Doesn't sound that good, does it? So git takes care of all of the hard work for you - it compares your local file with what is there in the repository you are working with, and can show it to you as a "diff", or apply that "diff" as a patch aka commit your changes, or even let you apply some patch file that you have already. Without going deep into details, in this sense git is absolutely the same as other version control systems like SVN, or even CVS or perforce.

用 git 说话时,补丁文件仍然意味着同样的事情,但自己使用 diff + patch 将是一场噩梦。例如,您将始终必须检出文件的两个版本(甚至整个存储库)才能进行比较。听起来不太好,是吗?因此,git 会为您处理所有繁重的工作——它将您的本地文件与您正在使用的存储库中的内容进行比较,并可以将其作为“差异”显示给您,或者将该“差异”应用为补丁又名提交您的更改,甚至让您应用一些您已经拥有的补丁文件。在不深入细节的情况下,从这个意义上讲,git 与其他版本控制系统(如 SVN,甚至 CVS 或 perforce)完全相同。

Hope it helps!

希望能帮助到你!

回答by Tom van der Woerdt

A patch is a small file that indicates the changes made in a repository. It's generally used when someone from outside your team has read-only access but had a good code change available. He then creates a patch and sends it to you. You apply it and push it to the git repository. Everyone then benefits from the updated version, and the author of the patch didn't need read/write access.

补丁是一个小文件,指示在存储库中所做的更改。它通常用于团队外部的人员具有只读访问权限但可以进行良好的代码更改时。然后他创建一个补丁并将其发送给您。您应用它并将其推送到 git 存储库。然后每个人都从更新的版本中受益,补丁的作者不需要读/写访问权限。

It's really mainly a security thing (at least, that's what people use it for).

它实际上主要是为了安全(至少,这是人们使用它的目的)。

回答by Anuraj

A patch file represents a single set of changes that can be applied to any branch, in any order. By using patch, you will get differences between one or more files. And later, you can apply the differences (patch) to get the changes on new files. There are many uses for a patch in Git. If you have uncommitted changes in your working directory, and you need to get that changes to apply somewhere else, just create a patch and apply the patch.

补丁文件代表可以以任何顺序应用于任何分支的一组更改。通过使用补丁,您将获得一个或多个文件之间的差异。稍后,您可以应用差异(补丁)来获取对新文件的更改。Git 中的补丁有很多用途。如果您的工作目录中有未提交的更改,并且您需要将这些更改应用到其他地方,只需创建一个补丁并应用该补丁即可。

git diff > mypatch.patch

If you have new files in your repository(untracked), then you should stage the file before creating a patch (don't commit), and use the following command

如果您的存储库中有新文件(未跟踪),那么您应该在创建补丁之前暂存该文件(不要提交),并使用以下命令

git diff --cached > mypatch.patch 

You can later apply the patch:

您可以稍后应用补丁:

git apply mypatch.patch

If you want to make some changes to a git repository, that you don't have a write permission, just make the changes and create a patch between both, and send the patch to someone who has the permission to apply the patch, by this your changes should be added to that git repository.

如果您想对 git 存储库进行一些更改,而您没有写入权限,只需进行更改并在两者之间创建补丁,然后将补丁发送给有权应用补丁的人,通过这个您的更改应该添加到该 git 存储库中。

回答by Steve Rukuts

A patch is a set of differences between one or more files, to show what is different between them. You would typically only generate a patch to show someone what you have changed. An example of when you might do this is when you find and fix a bug in an open source application and then post the fix on their bug tracker.

补丁是一个或多个文件之间的一组差异,以显示它们之间的不同之处。您通常只会生成一个补丁来向其他人展示您所做的更改。您可能会这样做的一个例子是,当您在开源应用程序中发现并修复错误,然后将修复发布到他们的错误跟踪器时。