zoukankan      html  css  js  c++  java
  • 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) {

    PS:如果您想和业内技术大牛交流的话,请加qq群(521249302)或者关注微信公众 号(AskHarries),谢谢!

  • 相关阅读:
    k8s之docker被屏蔽后下载方法
    代码合并工具Beyond Compare的使用技巧
    (转)设计模式——观察者模式
    notepad问题汇总
    个人构建问题
    内部git常用总结
    (转)动态规划算法—买卖股票的最佳时机系列
    (转)java中引用传递和值传递
    互联网面试题
    (转)UML类图与类的关系详解
  • 原文地址:https://www.cnblogs.com/kluan/p/6021380.html
Copyright © 2011-2022 走看看