多应用+插件架构,代码干净,支持一键云编译,码云点赞13K star,4.8-4.12 预售价格198元 广告
补丁是文本文件,其内容是相似于Git diff,但随着代码,它也有元数据有关提交,如提交ID,日期,提交信息等,我们可以创建补丁提交和其他人可以将它们应用到自己的资料库。 Byron 为他们的项目实现strcat函数。 Byron 可以创建自己的代码路径发送到Sampson。那么他就可以收到Byron 的代码补丁。 杰里使用Git format-patch 命令来创建最新提交的补丁。如果想创建补丁具体提交,然后使用COMMIT_ID 和 ormat-patch 命令。 [byron@CentOS project]$ pwd /home/byron/byron_repo/project/src [byron@CentOS src]$ git status -s M string_operations.c ?? string_operations [byron@CentOS src]$ git add string_operations.c [byron@CentOS src]$ git commit -m "Added my_strcat function" [master b4c7f09] Added my_strcat function 1 files changed, 13 insertions(+), 0 deletions(-) [byron@CentOS src]$ git format-patch -1 0001-Added-my_strcat-function.patch 上面的命令创建 .patch文件里在当前工作目录。 Sampson可以使用这个补丁修改他的文件。 Git提供两个命令来应用补丁调幅分别为: git am 和 git apply . Git apply命令修改本地文件时,而无需创建提交,git am命令修改文件,会一并创建提交。 适用于修补程序并创建提交使用下面的命令。 [sampson@CentOS src]$ pwd /home/sampson/top_repo/project/src [sampson@CentOS src]$ git diff [sampson@CentOS src]$ git status –s [sampson@CentOS src]$ git apply 0001-Added-my_strcat-function.patch [sampson@CentOS src]$ git status -s M string_operations.c ?? 0001-Added-my_strcat-function.patch 补丁得到成功应用,现在我们可以使用git diff命令查看修改。 [sampson@CentOS src]$ git diff 上面的命令会产生以下结果。 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) {