欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

易百教程 Git 补丁操作

补丁是文本文件,其内容是相似于Git diff,但随着代码,它也有元数据有关提交,如提交ID,日期,提交信息等,我们可以创建补丁提交和其他人可以将它们应用到自己的资料库。

Jerry 为他们的项目实现strcat函数。 Jerry 可以创建自己的代码路径发送到Tom。那么他就可以收到Jerry 的代码补丁。

杰里使用Git format-patch 命令来创建最新提交的补丁。如果想创建补丁具体提交,然后使用COMMIT_ID 和 ormat-patch 命令。

[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文件里在当前工作目录。 Tom可以使用这个补丁修改他的文件。 Git提供两个命令来应用补丁调幅分别为: git am 和 git apply . Git apply命令修改本地文件时,而无需创建提交,git am命令修改文件,会一并创建提交。

适用于修补程序并创建提交使用下面的命令。

[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命令查看修改。

[tom@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)
{

本站代码下载:http://www.yiibai.com/siteinfo/download.html

本文属作者原创,转载请注明出处:易百教程 » Git 补丁操作

普通分类: