From charlesreid1

Creating Patches

Creating patches with diff

Creating patches with svn

Creating patches from Subversion is straightforward because it automatically outputs in a format that patch can understand and use.

To create a patch file of a source code tree:

cd /path/to/source
svn diff . > ~/patches/patch.file

For example, if I add a comment to a source code file called Arches.cc:

$ cd /path/to/src

$ svn status .
M      CCA/Components/Arches/Arches.cc

$ svn diff .
Index: CCA/Components/Arches/Arches.cc
===================================================================
--- CCA/Components/Arches/Arches.cc	(revision 47166)
+++ CCA/Components/Arches/Arches.cc	(working copy)
@@ -1,5 +1,7 @@
 /*
 
+Added a comment
+
 The MIT License
 
 Copyright (c) 1997-2010 Center for the Simulation of Accidental Fires and 

then I can see the change in svn diff. It shows the file that was changed, and the line(s) at which the change(s) happened.

The output of svn diff can be piped to a file, which is then a patch file:

$ cd /path/to/src

$ svn diff . > patch_file

This patch file can then be shared with others, emailed, made public, etc., so that others can use the changes that you've made.

To apply the patch, see below.


Applying Patches

Apply a patch by running:

cd /path/to/source
patch -p0 < patch_file

The -p flag tells patch how many levels of directories to strip from the files listed in the patch file. So, if a patch file contains a file path like:

/dir1/dir2/dir3/dir4

then using -p1 strips dir1 off, using -p2 strips dir2 off, using -p3 strips dir3 off, etc.

This is usually not necessary if you're creating and applying patches from directories with common structures (this is usually the case with svn).