Git 简明教程

Git - Patch Operation

补丁是一个文本文件,其内容类似于 Git diff,但在代码的同时,还包含有关提交的元数据;例如,提交 ID、日期、提交消息等。我们能够从提交中创建一个补丁,其他人能够将其应用到自己的存储库中。

Patch is a text file, whose contents are similar to Git diff, but along with code, it also has metadata about commits; e.g., commit ID, date, commit message, etc. We can create a patch from commits and other people can apply them to their repository.

杰瑞为自己的项目实现了 strcat 函数。杰瑞能够创建他的代码的路径并将它发送给汤姆。然后,他能够将收到的补丁应用到自己的代码中。

Jerry implements the strcat function for his project. Jerry can create a path of his code and send it to Tom. Then, he can apply the received patch to his code.

杰瑞使用 Git format-patch 命令为最新的提交创建了一个补丁。如果你想要为某个特定的提交创建补丁,那么请与格式化补丁命令一起使用 COMMIT_ID

Jerry uses the Git format-patch command to create a patch for the latest commit. If you want to create a patch for a specific commit, then use COMMIT_ID with the format-patch command.

[jerry@CentOS project]$ pwd
/home/jerry/jerry_repo/project/src

[jerry@CentOS src]$ git status -s
M string_operations.c
?? string_operations

[jerry@CentOS src]$ git add string_operations.c

[jerry@CentOS src]$ git commit -m "Added my_strcat function"

[master b4c7f09] Added my_strcat function
1 files changed, 13 insertions(+), 0 deletions(-)

[jerry@CentOS src]$ git format-patch -1
0001-Added-my_strcat-function.patch

上述命令在当前工作目录中创建 .patch 文件。汤姆能够使用此补丁修改他的文件。Git 提供了两个命令应用补丁 git am*and *git applyGit apply 修改本地文件而不会创建提交,而 git am 修改文件并创建提交。

The above command creates .patch files inside the current working directory. Tom can use this patch to modify his files. Git provides two commands to apply patches git am*and *git apply, respectively. Git apply modifies the local files without creating commit, while git am modifies the file and creates commit as well.

要应用补丁并创建提交,请使用以下命令:

To apply patch and create commit, use the following command −

[tom@CentOS src]$ pwd
/home/tom/top_repo/project/src

[tom@CentOS src]$ git diff

[tom@CentOS src]$ git status –s

[tom@CentOS src]$ git apply 0001-Added-my_strcat-function.patch

[tom@CentOS src]$ git status -s
M string_operations.c
?? 0001-Added-my_strcat-function.patch

该补丁已成功应用,现在我们可以使用 git diff 命令查看修改。

The patch gets applied successfully, now we can view the modifications by using the git diff command.

[tom@CentOS src]$ git diff

以上命令将生成以下结果 −

The above command will produce the following result −

diff --git a/src/string_operations.c b/src/string_operations.c
index 8ab7f42..f282fcf 100644
--- a/src/string_operations.c
+++ b/src/string_operations.c
@@ -1,5 +1,16 @@
#include <stdio.h>
+char *my_strcat(char *t, char *s)
diff --git a/src/string_operations.c b/src/string_operations.c
index 8ab7f42..f282fcf 100644
--- a/src/string_operations.c
+++ b/src/string_operations.c
@@ -1,5 +1,16 @@
#include <stdio.h>
+char *my_strcat(char *t, char *s)
+
{
   +
   char *p = t;
   +
   +
   +
   while (*p)
   ++p;
   +
   while (*p++ = *s++)
   + ;
   + return t;
   +
}
+
size_t my_strlen(const char *s)
{
   const char *p = s;
   @@ -23,6 +34,7 @@ int main(void)
   {