linux下使用diff制作补丁,使用patch打补丁

我们使用diff命令制作补丁,使用patch命令打补丁。


(1)对单个文件操作,例如有如下两个源文件:

    test1.c:

#include <stdio.h>
#include <stdlib.h>

int main () {
    int a = 1;

    printf("value a: %d\n", a);

    return 0;
}
    test2.c:

#include <stdio.h>
#include <stdlib.h>

int main () {
    int a = 1;
    int b = 2;

    printf("value a: %d\n", a);
    printf("value b: %d\n", b);

    return 0;
}

生成从test1.c 到test2.c的补丁: diff -u test1.c test2.c > test.patch,则生成了补丁文件:

--- test1.c	2017-10-11 15:07:06.197500113 +0800
+++ test2.c	2017-10-11 15:06:46.138506900 +0800
@@ -3,8 +3,10 @@
 
 int main () {
     int a = 1;
+    int b = 2;
 
     printf("value a: %d\n", a);
+    printf("value b: %d\n", b);
 
     return 0;
 }

对test1.c打补丁:patch -p0 < test.patch,则可以看到 patching file test1.c的打印,表示补丁已经打成功。


(2)对文件夹操作,例如有dir1 dir2两个目录:

    使用diff制作补丁: diff -uNr dir1 dir2 > dir.patch

    使用patch打补丁:cd dir2

                                  patch -p1 < dir.patch


    说明:由于文件夹操作,可能有多级子目录,所以需要递归-r, 可能存在文件删除或者新建,所以需要-N

              由于对文件夹打补丁,最外层目录无需操作,直接对内部其他文件 操作,所以需要-p1忽略最外层目录,且需要先cd dir2



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值