对单个文件打patch
old.txt为原文件,new.txt 为已修改后的文件,现在要做的是:制作patch,更新old.txt文件
(1)查看文件内容
[wln@localhost 02] catold.txt123456qwertdasfgfdsaf[wln@localhost02] cat new.txt
1123456
qqwert
dasfg
dsaf
(2)制作patch
[wln@localhost 02] diff−Naurold.txtnew.txt>a.patch[wln@localhost02] cat a.patch
— old.txt 2014-06-09 15:35:34.000000000 +0800
+++ new.txt 2014-06-09 15:35:34.000000000 +0800
@@ -1,4 +1,4 @@
-123456
-qwert
+1123456
+qqwert
dasfg
-fdsaf
+dsaf
(3)diff参数解读
-N 选项确保补丁文件将正确地处理已经创建或删除文件的情况
-a 将所有文件都当做文本文件
-u 选项以统一格式创建补丁文件,这种格式比缺省格式更紧凑些
-r 是一个递归选项,设置了这个选项,diff会将两个不同版本源代码目录中的所有对应文件全部都进行一次比较,包括子目录文件
(4)执行patch
[wln@localhost 02] patch−p0<a.patchpatchingfileold.txt这里−p0表示执行patch所在的路径就是制作patch所在的路径。如果为−p1表示执行patch所在的路径为制作patch所在路径的子目录下,如果为−p2则为子目录的子目录下。所以最好是在哪里制作在哪里执行patch(用−p0)[wln@localhost02] cat old.txt
1123456
qqwert
dasfg
dsaf
[wln@localhost 02] catnew.txt1123456qqwertdasfgdsaf(5)打好patch,发现有问题,想返回,则执行−R[wln@localhost02] patch -R -p0 < a.patch
patching file old.txt
[wln@localhost 02] catold.txt123456qwertdasfgfdsaf[wln@localhost02] cat new.txt
1123456
qqwert
dasfg
dsaf多个文件打patch
(1)查看文件内容
[wln@localhost patch] ll总计8drwxrwxr−x3wlnwln409606−0915:48newdrwxrwxr−x3wlnwln409606−0915:48old[wln@localhostpatch] tree
.
|– new
|-- a
– b
| |-- a.txt
|
|-- foo.txt
– old
-- a
– b
-- foo.txt
– a
[wln@localhost patch]$ cat old/a/b/foo.txt
old_line_1
old_line_2
12324
[wln@localhost patch]$ cat new/a/a.txt
fdfas
fafes
1233
5678
2345
[wln@localhost patch]$ cat new/a/b/foo.txt
new_line_1
new_line_2
(2)制作patch
[wln@localhost patch]$ diff -Naur old new > a.patch
[wln@localhost patch]$ cat a.patch
diff -Naur old/a/a.txt new/a/a.txt
--- old/a/a.txt 1970-01-01 08:00:00.000000000 +0800
+++ new/a/a.txt 2014-06-09 15:48:45.000000000 +0800
@@ -0,0 +1,5 @@
+fdfas
+fafes
+1233
+5678
+2345
diff -Naur old/a/b/foo.txt new/a/b/foo.txt
--- old/a/b/foo.txt 2014-06-09 15:48:49.000000000 +0800
+++ new/a/b/foo.txt 2014-06-09 15:48:45.000000000 +0800
@@ -1,3 +1,2 @@
-old_line_1
-old_line_2
-12324
+new_line_1
+new_line_2
(3)执行patch
[wln@localhost patch]$ cd old/
[wln@localhost old]$ patch -p1 < ../a.patch
patching file a/a.txt
patching file a/b/foo.txt
[wln@localhost old]$ cd ../
[wln@localhost patch]$ tree
.
|-- a.patch
|-- new
|
| |– a.txt
|-- b
– foo.txt
|
-- old
– a
|– a.txt
-- b
– foo.txt
6 directories, 5 files
[wln@localhost patch]
catold/a/a.txtfdfasfafes123356782345[wln@localhostpatch]
cat old/a/b/foo.txt
new_line_1
new_line_2
[wln@localhost patch]
catnew/a/a.txtfdfasfafes123356782345[wln@localhostpatch]
cat new/a/b/foo.txt
new_line_1
new_line_2
[wln@localhost patch]
为什么要进入到old/下执行patch?因为有新文件a/a.txt生成,如果直接在制作patch路径执行patch,会产生问题。假如没有新文件生成,可以直接在制作patch的当前路径下执行patch(4)打好patch,发现有问题,想返回,则执行−R[wln@localhostpatch]
cd old/
[wln@localhost old]$ patch -R -p1 < ../a.patch
patching file a/a.txt
patching file a/b/foo.txt
转自:http://blog.csdn.net/huguangshanse00/article/details/35780081