The Ten Minute Guide to diff and patch

转载 2012年03月23日 18:16:13

The original article is here



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


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



在做开发的过程中难免需要给内核及下载的一些源码打补丁,所以我们先学习下Linux下使用如如何使用diff制作补丁以及如何使用patch打补丁。 通过diff命令比较出新旧版本之间的差异,并以pa...
  • tao_627
  • tao_627
  • 2016年10月26日 22:37
  • 1791

diff 和 patch 的入门(及 Windows 下的用法)

自从我开始使用 Git 以及接触 Linux 之后,我发现我越来越离不开 Linux 上那些方便好用的工具。比如 diff &patch。     如果修改了开源代码,为了方便分享(如提交漏洞)或自...
  • vigour1000
  • vigour1000
  • 2014年04月05日 12:22
  • 2935

Diff/Patch 工具的使用

补丁Patch是天才程序员、Perl的发明者Larry Wall发明的,它应高效地交流程序源代码之需求而生,随着以Linux为代表的开发源代码运行的蓬勃发展,patch这个概念已经成为开放源代码发起者...
  • zhanglianpin
  • zhanglianpin
  • 2014年06月09日 09:57
  • 4488


1,制作补丁 diff的使用 diff可以完成比较功能,生成补丁文件 格式::diff [option] oldfile newfile 常用的option选项有: -r 对目录进行递归处理 -u...
  • zqixiao_09
  • zqixiao_09
  • 2016年07月05日 21:31
  • 3329

diff & patch 制作及打补丁(两种生成补丁的方式(git和diff))

生成patch过程: 1. Git branch a 2. git checkout a 3. modify 4. git commit -am 5. git diff ...
  • u011784994
  • u011784994
  • 2016年10月27日 12:16
  • 4840


quilt命令具有生成补丁和应用补丁的功能,但它的优势是还可以作为管理补丁的工具。一个比较大的项目可能由不同开发者共同维护,其中很多修改都以补丁的方式提供,这些补丁可能存在依赖关系。 quilt工具在...
  • jasonchen_gbd
  • jasonchen_gbd
  • 2015年03月09日 23:02
  • 2224

Git Patch使用: 生成,导入,解决冲突

1. Git Patch git patch包括2中: 标准diff 和git format-patch 2. 生成patch 2.1 git diff 生成标准patch 最简单, 但是很多...
  • xiyanlgu
  • xiyanlgu
  • 2015年05月20日 14:48
  • 3167


谈及linux补丁(patch)及其patch操作,自然而然地觉其复杂,故一直存敬畏之心,所以也就一直远离它。但像做嵌入式linux开发,有时候patch是无法绕过的。 对于补丁制作与应用,实际上就...
  • mrwangwang
  • mrwangwang
  • 2013年07月22日 15:53
  • 932


最近要在hadoop2.4上面上线hdfs raid,但是之前hdfs raid是作为svn 的branch开发的,领导希望通过patch的方式将hdfs raid功能打进trunk里面去,这里涉及到...
  • hit_hlj_sgy
  • hit_hlj_sgy
  • 2014年11月20日 11:40
  • 1923


通过diff工具生成补丁, patch工具打上补丁. 在使用diff之前, 你需要保留一份未修改过的源码, 然后在其它地方修改源码的一份拷贝. diff对比这两份源码生成patch. 修改过的源码必...
  • yhcs1213
  • yhcs1213
  • 2015年10月21日 16:10
  • 797
您举报文章:The Ten Minute Guide to diff and patch