The Ten Minute Guide to diff and patch

Situation one: you are trying to compile a package from source and you discover that somebody has already done the work for you of modifying it slightly to compile on your system. They have made their work available as a "patch", but you‘re not sure how to make use of it. The answer is that you apply the patch to the original source code with a command line tool called, appropriately, patch.

Situation two: you have downloaded the source code to an open source package and after an hour or so of minor edits, you manage to make it compile on your system. You would like to make your work available to other programmers, or to the authors of the package, without redistributing the entire modified package. Now you are in a situation where you need to create a patch of your own, and the tool you need is diff.

This is a quick guide to diff and patch which will help you in these situations by describing the tools as they are most commonly used. It tells you enough to get started right away. Later, you can learn the ins and outs of diff and patch at your leisure, using the man pages.

Applying patches with patch

To apply a patch to a single file, change to the directory where the file is located and call patch:

patch < foo.patch

These instructions assume the patch is distributed in unified format, which identifies the file the patch should be applied to. If not, you can specify the file on the command line:

patch foo.txt < bar.patch

Applying patches to entire directories (perhaps the more common case) is similar, but you have to be careful about setting a "p level". What this means is that, within patch files, the files to be patched are identified by path names which may be different now that the files are located on your computer rather than on the computer where the patch was created. The p level instructs patch to ignore parts of the path name so that it can identify the files correctly. Most often a p level of one will work, so you use:

patch -p1 < baz.patch

You should change to the top level source directory before running this command. If a patch level of one does not correctly identify any files to patch, inspect the patch file for file names. If you see a name like

/users/stephen/package/src/net/http.c

and you are working in a directory that contains net/http.c, use

patch -p5 < baz.patch

In general, count up one for each path separator (slash character) that you remove from the beginning of the path, until what‘s left is a path that exists in your working directory. The count you reach is the p level.

To remove a patch, use the -R flag, ie

patch -p5 -R < baz.patch

Creating patches with diff

Using diff is simple whether you are working with single files or entire source directories. To create a patch for a single file, use the form:

diff -u original.c new.c > original.patch

To create a patch for an entire source tree, make a copy of the tree:

cp -R original new

Make any changes required in the directory new/. Then create a patch with the following command:

diff -rupN original/ new/ > original.patch

That‘s all you need to get started with diff and patch. For more information use:

man diff
man patch

Translations

This article has been translated and republished in the following languages:

Spanish: Guia de 10 minutos de diff y patch

Portuguese: O Guia de Dez Minutos de diff e patch

French: diff et patch en dix minutes

Polish: 10 minut z diff i patch

German diff und patch in zehn Minuten

Comment on this article and its topic

Google Friend Connect is going or gone so it has been turned off! Commenting will return if I find something good to replace it.



Copyright by Stephen Jungels. Written permission is required to repost or reprint this article

( from: http://jungels.net/articles/diff-patch-ten-minutes.html )

The Ten Minute Guide to diff and patch

时间: 2024-10-14 05:01:54

The Ten Minute Guide to diff and patch的相关文章

diff和patch使用指南

大纲 1.概述 2.diff的用法 3.patch的用法 4.实战演练 1.概述 diff和patch是一对工具,在数学上来说,diff是对两个集合的差运算,patch是对两个集合的和运算.     diff比较两个文件或文件集合的差异,并记录下来,生成一个diff文件,这也是我们常说的patch文件,即补丁文件.     patch能将diff文件运用于 原来的两个集合之一,从而得到另一个集合.举个例子来说文件A和文件B,经过diff之后生成了补丁文件C,那么着个过程相当于 A -B = C

&lt;转载&gt; diff 和 patch 命令

本文转载自:http://blog.chinaunix.net/uid-23390992-id-3312321.html diff与patch命令 diff与patch命令真可谓是天作之合,命令中的黄金搭档.老师讲了之后其实自己不是很懂,因为上课的时候没有好好听,上课走神了.怎么办呢,肯定不能放着不管了,而这两个命令确实很重要,所以只能厚着脸皮课后死啃资料自己学习咯,学了之后才有开头第一句的感慨. diff和patch是一对工具,数学上说,diff是对两个集合的差运算,patch是对两个集合的和

diff与patch的简单用法

diff 比较相似文件的差异(以行为单位),还可以生成用以更新的"补丁文件" 及指定目录下文件名的不同 diff比较两个目录下文件名的不一致 [[email protected] tmp]# ls aaaa my.cnf [[email protected] tmp]# ls bbbb myy.cnf [[email protected] tmp]# ls aaaa  bbbb  my.cnf  myyy.cnf [[email protected] tmp]# diff aaaa b

diff和patch的使用、patch文件的格式解说

为了弄懂 patch中的 p0   p1    和.orig文件是啥,找到了这篇文章! 来源:http://www.cnblogs.com/super119/archive/2010/12/18/1909941.html diff和patch的使用.patch文件的格式解说 diff diff是生成源码补丁的必备工具.其命令格式为: diff [命令行选项] 原始文件 新文件 常用命令行选项如下: -r 递归处理目录 -u 输出统一格式(unified format) -N patch里包含新文

用Diff和Patch工具维护源码

在Unix系统下,维护源码版本可以使用很多方法,其中最常用的当然是大名鼎鼎的CVS,但实际上,简单的版本维护工作并没有必要使用复杂的CVS等专门的版本维护工具,Unix标配中的diff和patch工具就完全可以完成代码的简单备份和升级工作. diff以"行"为单位比较两个文本文件(也可以是目录比较),并将不同之处以某种格式输出到标准输出上:patch可以读入这种输出,并按照一定指令使源文件(目录)按照目标文件(目录)更新.Linux内核源码就是按照这种方式保持更新的,我们在www.ke

diff 和 patch 双剑合璧

diff可以比较两个文件的不同,也可以比较两个文件夹的不同,并可以生成补丁文件 # 准备俩不同的文件 [[email protected] tmp]# cat old A:Hello,Good morning B:Hello,How are you? I am Hai Meimei. A:Fine. Tank you, and you? B:I am fine too. [[email protected] tmp]# cat new A:Hello,Good morning B:Hello,H

Linux中的版本控制---diff和patch命令

一.构造两个用于测试的文件 hello.txt: world.txt: 二.用diff命令比较两个文本文件的差异 对这个两个文本文件执行diff‘命令,并通过输出重定向,将差异保存在diff.txt文件中 $ diff -u hello.txt world.txt > diff.txt 参数-u是使得差异输出中带有上下文 第1,2行中的三个减号标识原始文件,三个加号标识目标文件: 第3行表示在这个差异小节中,是从原始文件的第1行开始之后的3行,目标文件从第1行到第3行: 从第4行开始的减号表示只

git apply、git am打补丁.diff 和 .patch【转】

本文转载自:https://www.jianshu.com/p/e5d801b936b6 前提: 生成patch: git format-patch -M master 生成指定patch,0163bed3bf59ae74c36cc5138b4c24f1556d8304是commit id,-1是指从当前id开始,向下提交次数,包含此次且计数从1开始. 也就是说,我想要打出0163bed3bf59ae74c36cc5138b4c24f1556d8304当前的patch,则: git format

Linux中的Diff和Patch

转自:https://www.cnblogs.com/cocowool/p/6409643.html 本文主要记录两个命令的学习情况:diff 和 patch.diff 和 patch 是一对工具,使用这对工具可以获取更新文件与历史文件的差异,并将更新应用到历史文件上.在数学上说,diff就是对两个集合的差运算,patch就是对两个集合的和运算. 简单的例子 使用这个例子来说明如何进行文件的对比和打补丁. 这里有两个文件 original.txt 和 updated.txt,如下: #inclu