找两段不一样的代码:
如:
test.c
#include <gd.h>
#include <gdfontl.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
return 0;
}
test_new.c
#include <gd.h>
#include <gdfontl.h>
#include <stdio.h>
int main(int argc, char **argv)
{
/* Declare the image */
gdImagePtr im;
return 0;
}
制作补丁:
diff -uN test.c test1.c > 1.patch
打补丁:
patch -p0<1.patch
如:
test.c
#include <gd.h>
#include <gdfontl.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
return 0;
}
test_new.c
#include <gd.h>
#include <gdfontl.h>
#include <stdio.h>
int main(int argc, char **argv)
{
/* Declare the image */
gdImagePtr im;
return 0;
}
制作补丁:
diff -uN test.c test1.c > 1.patch
打补丁:
patch -p0<1.patch
test.c即可变成test1.c的内容。
还原:
此时如果你再次使用patch命令,系统会问你是否想还原,输入y 还原:
# patch -p0 < foo.patch
patching file old/a/b/foo.txt
Reversed (or previously applied) patch detected! Assume -R? [n] y
还原补丁
如果你想严格指定是 还原补丁 可以使用下面命令(就是增加R参数):
# patch -Rp0 < foo.patch
解释:
diff
-r 是一个递归选项,设置了这个选项,diff会将两个不同版本源代码目录中的所有对应文件全部都进行一次比较,包括子目录文件。
-N 选项确保补丁文件将正确地处理已经创建或删除文件的情况。
-u 选项以统一格式创建补丁文件,这种格式比缺省格式更紧凑些。
patch
patch就是利用diff制作的补丁来实现源文件(夹)和目的文件(夹)的转换。这样说就意味着你可以有源文件(夹)――>目的文件(夹),也可以目的文件(夹)――>源文件(夹)。下面介绍几个最常用选项:
-p0 选项要从当前目录查找目的文件(夹)
-p1 选项要忽略掉第一层目录,从当前目录开始查找。
-E 选项说明如果发现了空文件,那么就删除它
-R 选项说明在补丁文件中的“新”文件和“旧”文件现在要调换过来了(实际上就是给新版本打补丁,让它变成老版本)