首先介绍一下diff和patch。在这里不会把man在线文档上所有的选

一、diff结果及patch文件的详解

cat before.txt

  输出:

  This is a line to be deleted

  This is a line that will be changed

  This is a line that will be unchanged

  cat after.txt

  输出:

  This is a line that has been changed

  This is a line that will be unchanged

  This is a line that has been added

  ############################################

  diff before.txt after.txt

  输 出:

  1,2c1

  < This is a line to be deleted

  < This is a line that will be changed

  ---

  > This is a line that has been changed

  3a3

  > This is a line that has been added

  ############################################

  注释:

  传统格式的输出

  1,2c1是指替换第1个文件的第1,2行到第2个文件的第2行,这里的1,2是指第1个文件的第1,2行,c是替换的意思,最后的1是第2个文件的第1行

  <号是指第1个文件更改或删除的行

  ---号是分割两个文件

  >号是第2个文件中增加或删除的行

  3a3是指将第2个文件的第3行插入到第一个文件的第3行

  也就是说第1个文件的:

  < This is a line to be deleted

  < This is a line that will be changed

  被替换成第2个文件的:

  > This is a line that has been changed

  由于第1个文件的第3行和第2个文件的第2行一致,所以不做修改.

  由于第2个文件的第3行是第1个文件所不具有的,所以在第1个文件的最后一行增加:

  > This is a line that has been added2)patch命令的应用

  用diff的传统格式输出:

  #################################

  diff before.txt after.txt >mypatch.txt

  #################################

  用patch修补before.txt文件,使before.txt和after.txt一致.

  #################################

  cat mypatch.txt |patch before.txt

  输出:

  patching file before.txt

  #################################

  比较两个文件,现在是一致的了.

  #################################

  cmp before.txt after.txt

  #################################

  用patch命令恢复before.txt.

  #################################

  patch -R before.txt <mypatch.txt

  输出:

  patching file before.txt

  #################################

  注:-R标记告诉patch在反向上应用区别或者撤销patch.

  再比较两个文件,现在不一致了.

  #################################

  cmp before.txt after.txt

  输出:

  before.txt after.txt differ: byte 17, line 1

diff -u before.txt after.txt |tee mypatch.diff

  输出:

  --- before.txt  2009-06-20 05:21:49.000000000 +0800

  +++ after.txt   2009-06-20 04:03:16.000000000 +0800

  @@ -1,3 +1,3 @@

  -This is a line to be deleted

  -This is a line that will be changed

  +This is a line that has been changed

  This is a line that will be unchanged

  +This is a line that has been added

  #################################

  注释:

  diff -u选项是统一格式输出.

  --- before.txt  2009-06-20 05:21:49.000000000 +0800

  --- before.txt是指旧文件

  +++ after.txt   2009-06-20 04:03:16.000000000 +0800

  +++ after.txt是指新文件.

  @@ -1,3 +1,3 @@

  @@ -1,3是指第1个文件一共有3行,+1,3 @@是指第2个文件一共有3行.

  -This is a line to be deleted

  -This is a line that will be changed

  是被删除的行

  +This is a line that has been changed

  是增加的行

  This is a line that will be unchanged

  没有-号和+号是指该行不变,因为after.txt和before.txt都有这行.

  +This is a line that has been added

  是增加的行

  diff的统一格式比较与输出是按顺序进行的.

  4)diff命令在目录中的应用.

  新建old和new目录,old目录包含了初始内容,new目录包含文件的最新版本.

  ##########################################

  mkdir old new

  echo "This is one. It's unchanged." | tee old/one new/one

  echo "This is two. It will change." > old/two

  echo "This is two. It changed.">new/two

  echo "This is three. It's new" > new/three

  ##########################################

  创建修补文件

  ##########################################

  diff -Nur old/ new/ >mypatch.diff

  ##########################################

  注:-r选项按照文件目录递归创建修补文件.

  -u还是统一模式

  -N是指当diff遇到一个只存在于两个树中的一个树中的文件时,默认情况下跳过文件并且打印一个警告到stderr.

  这个行为可以通过-N选项来更改,这也导致了diff认为丢失的文件实际上是存在的,但它是空的.采用这种方式,

  一个修补文件可以包括已经创建的文件.然后应用修补程序创建新的文件.

  ##########################################

  more mypatch.diff

  输出:

  diff -Nur old/three new/three

  --- old/three   1970-01-01 08:00:00.000000000 +0800

  +++ new/three   2009-06-20 06:55:34.000000000 +0800

  @@ -0,0 +1 @@

  +This is three. It's new

  diff -Nur old/two new/two

  --- old/two     2009-06-20 06:55:08.000000000 +0800

  +++ new/two     2009-06-20 06:55:21.000000000 +0800

  @@ -1 +1 @@

  -This is two. It will change.

  +This is two. It changed.

  ##########################################

注释:

  diff -Nur old/three new/three是指下面比较old/three new/three两个文件.

  因为没有old/three文件,所以在old/three中增加+This is three. It's new

  diff -Nur old/two new/two是指下面比较old/two new/two两个文件

  因为old/two与new/two的第3行不一致,所以删除This is two. It will change.增加This is two. It changed.

  打补丁到old目录,新建old/three以及更改old/two

  ##########################################

  patch --dir old< mypatch.diff

  ls -l     old/

  输出:

  one    three  two

  ##########################################

  恢复old目录的内容,包括删除old/three,以及恢复old/two文件

  ##########################################

  patch --dir old -R <mypatch.diff

  输出:

  ls -l old/

  one  two

  ##########################################

  5)检查和合并更改

  用vim突出显示单个字符的更改来表示区别.

  ##########################################

  vim -d after.txt before.txt

  ##########################################

  用gui工具gvimdiff来显示两个文件.

  ##########################################

  gvimdiff after.txt before.txt

  ##########################################

  新建文件orig.c

  ##########################################

  vi orig.c

  void foo(void)

  {

  printf("This will be changed by me. n");

  printf("This will be unchanged,n");

  printf("This will be changed by you.n");

  }

  ##########################################

  复制文件orig.c到me.c,更改第4行为printf("This was changed by me. n");

  ##########################################

  vi me.c

  void foo(void)

  {

  printf("This was changed by me. n");

  printf("This will be unchanged,n");

  printf("This will be changed by you.n");

  }

  ##########################################

  复制文件orig.c到you.c,更改第7行为printf("This was changed by you.n");

  ##########################################

  vi you.c

  void foo(void)

  {

  printf("This will be changed by me. n");

  printf("This will be unchanged,n");

  printf("This was changed by you.n");

  }

diff3 me.c orig.c you.c

  输出:

  ====1

  1:3c

  printf("This was changed by me. n");

  2:3c

  3:3c

  printf("This will be changed by me. n");

  ====3

  1:7c

  2:7c

  printf("This will be changed by you.n");

  3:7c

  printf("This was changed by you.n");

  注:

  在没有参数的情况下,diff3产生的输出说明了那行更改.

  ====1和====3指明造成同原始文件不同的是哪一个修改文件.

  编号方式基于参数序列.

  也就是第1个文件和第3个文件与原文件不同.

  1:3c

  printf("This was changed by me. n");

  3:3c

  printf("This will be changed by me. n");

  1:3c表示第1个文件的第3行与3:3c表示的第3个文件的第3行不同.

  为什么不显示与原文件的比较呢。因为第3个文件的第3行与源文件(第2个文件)相同.所以与哪个文件比较无所谓了.

  2:7c

  printf("This will be changed by you.n");

  3:7c

  printf("This was changed by you.n");

  2:7c表示第2个文件的第7行与3:7c表示的第3个文件的第7行不同.

  diff3会试图为我们进行合并.合并是在源文件的基础上,依据两个新文件进行修改

  源文件是第二个文件,第一个文件和第三个文件可以互换,但他们必须有共同的祖先,就是第二个文件.

  #######################################

  diff3 -m me.c orig.c you.c |cat -n

  输出:

  1    void foo(void)

  2    {

  3        printf("This was changed by me. n");

  4

  5        printf("This will be unchanged,n");

  6

  7        printf("This was changed by you.n");

  8    }

  ########################################为了测试更复杂的环境,新建一个文件orig.c.1

  内容如下:

  ########################################

  vi orig.c.1

  void foo(void)

  {

  printf("This will be changed by both of us.n");

  }

  ########################################

  用diff3 -m再次比较输出,如下:

  ########################################

  diff3 -m me.c orig.c.1 you.c

  void foo(void)

  {

  <<<<<<< me.c

  printf("This was changed by me. n");

  printf("This will be unchanged,n");

  printf("This will be changed by you.n");

  ||||||| orig.c.1

  printf("This will be changed by both of us.n");

  =======

  printf("This will be changed by me. n");

  printf("This will be unchanged,n");

  printf("This was changed by you.n");

  >>>>>>> you.c

  }


首先介绍一下diff和patch。在这里不会把man在线文档上所有的选项都介绍一下,那样也没有必要。在99%的时间里,我们只会用到几个选项。所以必须学会这几个选项。

1、diff
    --------------------
    NAME
           diff - find differences between two files
    SYNOPSIS
           diff [options] from-file to-file
    --------------------

    简 单的说,diff的功能就是用来比较两个文件的不同,然后记录下来,也就是所谓的diff补丁。语法格式:diff 【选项】 源文件(夹) 目的文件(夹),就是要给源文件(夹)打个补丁,使之变成目的文件(夹),术语也就是“升级”。下面介绍三个最为常用选项:

    -r 是一个递归选项,设置了这个选项,diff会将两个不同版本源代码目录中的所有对应文件全部都进行一次比较,包括子目录文件。
    -N 选项确保补丁文件将正确地处理已经创建或删除文件的情况。
    -u 选项以统一格式创建补丁文件,这种格式比缺省格式更紧凑些。

2、patch
    ------------------
    NAME
       patch - apply a diff file to an original
    SYNOPSIS
           patch [options] [originalfile [patchfile]]
    
       but usually just
    
       patch -pnum <patchfile>
    
------------------
    简单的说,patch就是利用diff制作的补丁来实现源文件(夹)和目的文件(夹)的转换。这样说就意味着你可以有源文件(夹)――>目的文件(夹),也可以目的文件(夹)――>源文件(夹)。下面介绍几个最常用选项:
    -p0 选项要从当前目录查找目的文件(夹)
    -p1 选项要忽略掉第一层目录,从当前目录开始查找。
************************************************************
在这里以实例说明:
--- old/modules/pcitable       Mon Sep 27 11:03:56 1999
+++ new/modules/pcitable       Tue Dec 19 20:05:41 2000
    如果使用参数-p0,那就表示从当前目录找一个叫做old的文件夹,在它下面寻找modules下的pcitable文件来执行patch操作。
    如果使用参数-p1, 那就表示忽略第一层目录(即不管old),从当前目录寻找modules的文件夹,在它下面找pcitable。这样的前提是当前目 录必须为modules所在的目录。而diff补丁文件则可以在任意位置,只要指明了diff补丁文件的路径就可以了。当然,可以用相对路径,也可以用绝 对路径。不过我一般习惯用相对路径。
************************************************************

-E 选项说明如果发现了空文件,那么就删除它

-R 选项说明在补丁文件中的“新”文件和“旧”文件现在要调换过来了(实际上就是给新版本打补丁,让它变成老版本)
下面结合具体实例来分析和解决,分为两种类型:为单个文件打补丁和为文件夹内的多个文件打补丁。
环境:在RedHat 9.0下面以armlinux用户登陆。
目录树如下:
    |-- bootloader
    |-- debug
    |-- images
    |-- kernel
    |-- program
    |-- rootfiles
    |-- software
    |-- source
    |-- sysapps
    |-- tmp
    |-- tools
下面在program文件夹下面建立patch文件夹作为实验用,然后进入patch文件夹。
一、为单个文件进行补丁操作
1、建立测试文件test0、test1
[armlinux@lqm patch]$ cat >>test0<<EOF
> 111111
> 111111
> 111111
> EOF
[armlinux@lqm patch]$ more test0
111111
111111
111111
[armlinux@lqm patch]$ cat >>test1<<EOF
> 222222
> 111111
> 222222
> 111111
> EOF
[armlinux@lqm patch]$ more test1
222222
111111
222222
111111
2、使用diff创建补丁test1.patch
[armlinux@lqm patch]$ diff -uN test0 test1 > test1.patch
【注:因为单个文件,所以不需要-r选项。选项顺序没有关系,即可以是-uN,也可以是-Nu。】
[armlinux@lqm patch]$ ls
test0 test1 test1.patch
[armlinux@lqm patch]$ more test1.patch
************************************************************
patch文件的结构
补丁头
补丁头是分别由---/+++开头的两行,用来表示要打补丁的文件。---开头表示旧文件,+++开头表示新文件。
一个补丁文件中的多个补丁
一个补丁文件中可能包含以---/+++开头的很多节,每一节用来打一个补丁。所以在一个补丁文件中可以包含好多个补丁。

块是补丁中要修改的地方。它通常由一部分不用修改的东西开始和结束。他们只是用来表示要修改的位置。他们通常以@@开始,结束于另一个块的开始或者一个新的补丁头。
块的缩进
块会缩进一列,而这一列是用来表示这一行是要增加还是要删除的。
块的第一列
+号表示这一行是要加上的。
-号表示这一行是要删除的。
没有加号也没有减号表示这里只是引用的而不需要修改。
************************************************************
***diff命令会在补丁文件中记录这两个文件的首次创建时间,如下***
--- test0       2006-08-18 09:12:01.000000000 +0800
+++ test1       2006-08-18 09:13:09.000000000 +0800
@@ -1,3 +1,4 @@
+222222
111111
-111111
+222222
111111
[armlinux@lqm patch]$ patch -p0 < test1.patch
patching file test0
[armlinux@lqm patch]$ ls
test0 test1 test1.patch
[armlinux@lqm patch]$ cat test0
222222
111111
222222
111111
3、可以去除补丁,恢复旧版本
[armlinux@lqm patch]$ patch -RE -p0 < test1.patch
patching file test0
[armlinux@lqm patch]$ ls
test0 test1 test1.patch
[armlinux@lqm patch]$ cat test0
111111
111111
111111
二、为多个文件进行补丁操作
1、创建测试文件夹
[armlinux@lqm patch]$ mkdir prj0
[armlinux@lqm patch]$ cp test0 prj0
[armlinux@lqm patch]$ ls
prj0 test0 test1 test1.patch
[armlinux@lqm patch]$ cd prj0/
[armlinux@lqm prj0]$ ls
test0
[armlinux@lqm prj0]$ cat >>prj0name<<EOF
> --------
> prj0/prj0name
> --------
> EOF
[armlinux@lqm prj0]$ ls
prj0name test0
[armlinux@lqm prj0]$ cat prj0name
--------
prj0/prj0name
--------
[armlinux@lqm prj0]$ cd ..
[armlinux@lqm patch]$ mkdir prj1
[armlinux@lqm patch]$ cp test1 prj1
[armlinux@lqm patch]$ cd prj1
[armlinux@lqm prj1]$ cat >>prj1name<<EOF
> ---------
> prj1/prj1name
> ---------
> EOF
[armlinux@lqm prj1]$ cat prj1name
---------
prj1/prj1name
---------
[armlinux@lqm prj1]$ cd ..

2、创建补丁
[armlinux@lqm patch]$ diff -uNr prj0 prj1 > prj1.patch
[armlinux@lqm patch]$ more prj1.patch
diff -uNr prj0/prj0name prj1/prj0name
--- prj0/prj0name       2006-08-18 09:25:11.000000000 +0800
+++ prj1/prj0name       1970-01-01 08:00:00.000000000 +0800
@@ -1,3 +0,0 @@
---------
-prj0/prj0name
---------
diff -uNr prj0/prj1name prj1/prj1name
--- prj0/prj1name       1970-01-01 08:00:00.000000000 +0800
+++ prj1/prj1name       2006-08-18 09:26:36.000000000 +0800
@@ -0,0 +1,3 @@
+---------
+prj1/prj1name
+---------
diff -uNr prj0/test0 prj1/test0
--- prj0/test0 2006-08-18 09:23:53.000000000 +0800
+++ prj1/test0 1970-01-01 08:00:00.000000000 +0800
@@ -1,3 +0,0 @@
-111111
-111111
-111111
diff -uNr prj0/test1 prj1/test1
--- prj0/test1 1970-01-01 08:00:00.000000000 +0800
+++ prj1/test1 2006-08-18 09:26:00.000000000 +0800
@@ -0,0 +1,4 @@
+222222
+111111
+222222
+111111

[armlinux@lqm patch]$ ls
prj0 prj1 prj1.patch test0 test1 test1.patch
[armlinux@lqm patch]$ cp prj1.patch ./prj0
[armlinux@lqm patch]$ cd prj0
[armlinux@lqm prj0]$ patch -p1 < prj1.patch
patching file prj0name
patching file prj1name
patching file test0
patching file test1
[armlinux@lqm prj0]$ ls
prj1name prj1.patch test1
[armlinux@lqm prj0]$ patch -R -p1 < prj1.patch
patching file prj0name
patching file prj1name
patching file test0
patching file test1
[armlinux@lqm prj0]$ ls
prj0name prj1.patch test0
-------------------------------------------------
总结一下:
单个文件
diff –uN from-file to-file >to-file.patch
cat before.txt

  输出:

  This is a line to be deleted

  This is a line that will be changed

  This is a line that will be unchanged

  cat after.txt

  输出:

  This is a line that has been changed

  This is a line that will be unchanged

  This is a line that has been added

  ############################################

  diff before.txt after.txt

  输 出:

  1,2c1

  < This is a line to be deleted

  < This is a line that will be changed

  ---

  > This is a line that has been changed

  3a3

  > This is a line that has been added

  ############################################

  注释:

  传统格式的输出

  1,2c1是指替换第1个文件的第1,2行到第2个文件的第2行,这里的1,2是指第1个文件的第1,2行,c是替换的意思,最后的1是第2个文件的第1行

  <号是指第1个文件更改或删除的行

  ---号是分割两个文件

  >号是第2个文件中增加或删除的行

  3a3是指将第2个文件的第3行插入到第一个文件的第3行

  也就是说第1个文件的:

  < This is a line to be deleted

  < This is a line that will be changed

  被替换成第2个文件的:

  > This is a line that has been changed

  由于第1个文件的第3行和第2个文件的第2行一致,所以不做修改.

  由于第2个文件的第3行是第1个文件所不具有的,所以在第1个文件的最后一行增加:

  > This is a line that has been added2)patch命令的应用

  用diff的传统格式输出:

  #################################

  diff before.txt after.txt >mypatch.txt

  #################################

  用patch修补before.txt文件,使before.txt和after.txt一致.

  #################################

  cat mypatch.txt |patch before.txt

  输出:

  patching file before.txt

  #################################

  比较两个文件,现在是一致的了.

  #################################

  cmp before.txt after.txt

  #################################

  用patch命令恢复before.txt.

  #################################

  patch -R before.txt <mypatch.txt

  输出:

  patching file before.txt

  #################################

  注:-R标记告诉patch在反向上应用区别或者撤销patch.

  再比较两个文件,现在不一致了.

  #################################

  cmp before.txt after.txt

  输出:

  before.txt after.txt differ: byte 17, line 1

diff -u before.txt after.txt |tee mypatch.diff

  输出:

  --- before.txt  2009-06-20 05:21:49.000000000 +0800

  +++ after.txt   2009-06-20 04:03:16.000000000 +0800

  @@ -1,3 +1,3 @@

  -This is a line to be deleted

  -This is a line that will be changed

  +This is a line that has been changed

  This is a line that will be unchanged

  +This is a line that has been added

  #################################

  注释:

  diff -u选项是统一格式输出.

  --- before.txt  2009-06-20 05:21:49.000000000 +0800

  --- before.txt是指旧文件

  +++ after.txt   2009-06-20 04:03:16.000000000 +0800

  +++ after.txt是指新文件.

  @@ -1,3 +1,3 @@

  @@ -1,3是指第1个文件一共有3行,+1,3 @@是指第2个文件一共有3行.

  -This is a line to be deleted

  -This is a line that will be changed

  是被删除的行

  +This is a line that has been changed

  是增加的行

  This is a line that will be unchanged

  没有-号和+号是指该行不变,因为after.txt和before.txt都有这行.

  +This is a line that has been added

  是增加的行

  diff的统一格式比较与输出是按顺序进行的.

  4)diff命令在目录中的应用.

  新建old和new目录,old目录包含了初始内容,new目录包含文件的最新版本.

  ##########################################

  mkdir old new

  echo "This is one. It's unchanged." | tee old/one new/one

  echo "This is two. It will change." > old/two

  echo "This is two. It changed.">new/two

  echo "This is three. It's new" > new/three

  ##########################################

  创建修补文件

  ##########################################

  diff -Nur old/ new/ >mypatch.diff

  ##########################################

  注:-r选项按照文件目录递归创建修补文件.

  -u还是统一模式

  -N是指当diff遇到一个只存在于两个树中的一个树中的文件时,默认情况下跳过文件并且打印一个警告到stderr.

  这个行为可以通过-N选项来更改,这也导致了diff认为丢失的文件实际上是存在的,但它是空的.采用这种方式,

  一个修补文件可以包括已经创建的文件.然后应用修补程序创建新的文件.

  ##########################################

  more mypatch.diff

  输出:

  diff -Nur old/three new/three

  --- old/three   1970-01-01 08:00:00.000000000 +0800

  +++ new/three   2009-06-20 06:55:34.000000000 +0800

  @@ -0,0 +1 @@

  +This is three. It's new

  diff -Nur old/two new/two

  --- old/two     2009-06-20 06:55:08.000000000 +0800

  +++ new/two     2009-06-20 06:55:21.000000000 +0800

  @@ -1 +1 @@

  -This is two. It will change.

  +This is two. It changed.

  ##########################################

注释:

  diff -Nur old/three new/three是指下面比较old/three new/three两个文件.

  因为没有old/three文件,所以在old/three中增加+This is three. It's new

  diff -Nur old/two new/two是指下面比较old/two new/two两个文件

  因为old/two与new/two的第3行不一致,所以删除This is two. It will change.增加This is two. It changed.

  打补丁到old目录,新建old/three以及更改old/two

  ##########################################

  patch --dir old< mypatch.diff

  ls -l     old/

  输出:

  one    three  two

  ##########################################

  恢复old目录的内容,包括删除old/three,以及恢复old/two文件

  ##########################################

  patch --dir old -R <mypatch.diff

  输出:

  ls -l old/

  one  two

  ##########################################

  5)检查和合并更改

  用vim突出显示单个字符的更改来表示区别.

  ##########################################

  vim -d after.txt before.txt

  ##########################################

  用gui工具gvimdiff来显示两个文件.

  ##########################################

  gvimdiff after.txt before.txt

  ##########################################

  新建文件orig.c

  ##########################################

  vi orig.c

  void foo(void)

  {

  printf("This will be changed by me. n");

  printf("This will be unchanged,n");

  printf("This will be changed by you.n");

  }

  ##########################################

  复制文件orig.c到me.c,更改第4行为printf("This was changed by me. n");

  ##########################################

  vi me.c

  void foo(void)

  {

  printf("This was changed by me. n");

  printf("This will be unchanged,n");

  printf("This will be changed by you.n");

  }

  ##########################################

  复制文件orig.c到you.c,更改第7行为printf("This was changed by you.n");

  ##########################################

  vi you.c

  void foo(void)

  {

  printf("This will be changed by me. n");

  printf("This will be unchanged,n");

  printf("This was changed by you.n");

  }

diff3 me.c orig.c you.c

  输出:

  ====1

  1:3c

  printf("This was changed by me. n");

  2:3c

  3:3c

  printf("This will be changed by me. n");

  ====3

  1:7c

  2:7c

  printf("This will be changed by you.n");

  3:7c

  printf("This was changed by you.n");

  注:

  在没有参数的情况下,diff3产生的输出说明了那行更改.

  ====1和====3指明造成同原始文件不同的是哪一个修改文件.

  编号方式基于参数序列.

  也就是第1个文件和第3个文件与原文件不同.

  1:3c

  printf("This was changed by me. n");

  3:3c

  printf("This will be changed by me. n");

  1:3c表示第1个文件的第3行与3:3c表示的第3个文件的第3行不同.

  为什么不显示与原文件的比较呢。因为第3个文件的第3行与源文件(第2个文件)相同.所以与哪个文件比较无所谓了.

  2:7c

  printf("This will be changed by you.n");

  3:7c

  printf("This was changed by you.n");

  2:7c表示第2个文件的第7行与3:7c表示的第3个文件的第7行不同.

  diff3会试图为我们进行合并.合并是在源文件的基础上,依据两个新文件进行修改

  源文件是第二个文件,第一个文件和第三个文件可以互换,但他们必须有共同的祖先,就是第二个文件.

  #######################################

  diff3 -m me.c orig.c you.c |cat -n

  输出:

  1    void foo(void)

  2    {

  3        printf("This was changed by me. n");

  4

  5        printf("This will be unchanged,n");

  6

  7        printf("This was changed by you.n");

  8    }

  ########################################为了测试更复杂的环境,新建一个文件orig.c.1

  内容如下:

  ########################################

  vi orig.c.1

  void foo(void)

  {

  printf("This will be changed by both of us.n");

  }

  ########################################

  用diff3 -m再次比较输出,如下:

  ########################################

  diff3 -m me.c orig.c.1 you.c

  void foo(void)

  {

  <<<<<<< me.c

  printf("This was changed by me. n");

  printf("This will be unchanged,n");

  printf("This will be changed by you.n");

  ||||||| orig.c.1

  printf("This will be changed by both of us.n");

  =======

  printf("This will be changed by me. n");

  printf("This will be unchanged,n");

  printf("This was changed by you.n");

  >>>>>>> you.c

  }



patch –p0 < to-file.patch
patch –RE –p0 < to-file.patch

多个文件
diff –uNr from-docu to-docu >to-docu.patch
patch –p1 < to-docu.patch
patch –R –p1 <to-docu.patch

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值