103

I know there are several threads on this already, but no one has fully explained exactly how to perform the initial diff to create the patch file, then how to apply that patch to the initial directory to update it.

In my case, there is a directory of files that anyone can download from the web. I have taken that directory and made changes to it, and want to create a patch file such that others can apply it to the downloaded directory to reproduce exactly what I have in my modified directory.

Help? What do I need to tell the other person with respect to how to apply my patch?

4 Answers 4

190

I just had this same problem - lots of advice on how to half do it. Well, here is what I did to get both the patching and unpatching to work:

To Create the Patch File:

  1. Put copies of both directories in say /tmp, so we can create the patch file, or if brave, get them side by side - in one directory.

  2. Run an appropriate diff on the two directories, old and new:

    diff -ruN orig/ new/ > file.patch
    # -r == recursive, so do subdirectories
    # -u == unified style, if your system lacks it or if recipient
    #       may not have it, use "-c"
    # -N == treat absent files as empty
    

If a person has the orig/ directory, they can recreate the new one by running patch.

To Recreate the new folder from old folder and patch file:

  1. Move the patch file to a directory where the orig/ folder exists

  2. This folder will get clobbered, so keep a backup of it somewhere, or use a copy.

    patch -s -p0 < file.patch
    # -s == silent except errors
    # -p0 == needed to find the proper folder
    
  3. At this point, the orig/ folder contains the new/ content, but still has its old name, so:

    mv orig/ new/    # if the folder names are different
    
12
  • 1
    I'm a mac guy so no idea. What you have to do is check the options for patch and diff on Cygwin. That's why I added the comments above on what the options mean - so if one or the other program offers different options you can figure out what to change to get it to work. Conceptually all patch/diff programs should support the functionality.
    – David H
    Commented Feb 26, 2013 at 16:48
  • @CharanPai "diff" does not support binary files, so I assume not. You might be able to create your own command file wrapper to do this. What you would do is binhex your binary data files - create a text file for each in binhex or similar ascii format. Then diff those files, and after the patch is applied, unbinhex the (possibly revised) binhex file bad into binary.
    – David H
    Commented Jun 9, 2014 at 20:42
  • no, -a would work for that. treats all files as text.
    – Charan Pai
    Commented Jun 11, 2014 at 14:17
  • 1
    @DavidH is it possible to omit those outer directory names? Or is it neccessary that patch should contain new and orig directory names? Commented Aug 30, 2016 at 4:47
  • 2
    patch was patching the new/ not orig/ directory for me, but I found the -d option which allows you to say cd into that directory first before applying the patch and then you can adjust the -p N argument accordingly.
    – dramzy
    Commented Jan 30, 2017 at 3:42
14

I needed to create a patch file and send it to someone so they could update their directory to match mine. There are many caveats with diff and patch however, so it ended up taking me hours to figure out something so conceptually simple. Absolute paths seem to be preferred over relative paths, and many of the options seem to have evolved from niche use cases. I finally figured out a solution based on David H's answer, with additional tips from Lakshmanan Ganapathy):

  • Back up your directory to directory.orig
  • Modify your directory to reach the desired state
  • Save diff from directory.orig to directory in file.patch so name matches for recipient

Here are my notes:

# to create patch:
# copy <directory> backup to something like <directory>.orig alongside it
cp -r <path_to>/<directory> <path_to>/<directory>.orig
# create/update/delete files/folders in <directory> until desired state is reached
# change working directory to <directory>
cd <path_to>/<directory>
# create patch file alongside <directory>
diff -Naru ../<directory>.orig . > ../file.patch
# -N --new-file Treat absent files as empty.
# -a --text Treat all files as text.
# -r --recursive Recursively compare any subdirectories found.
# -u -U NUM --unified[=NUM] Output NUM (default 3) lines of unified context.

# to apply patch:
# change working directory to <directory>
cd <path_to>/<directory>
patch -s -p0 < <path_to>/file.patch
# -s or --silent or --quiet Work silently, unless an error occurs.
# -pN or --strip=N Strip smallest prefix containing num leading slashes from files.

# to undo patch (note that directories created by patch must be removed manually):
# change working directory to <directory>
cd <path_to>/<directory>
patch -Rs -p0 < <path_to>/file.patch
# -R or --reverse Assume that patch was created with the old and new files swapped.
# -s or --silent or --quiet Work silently, unless an error occurs.
# -pN or --strip=N Strip smallest prefix containing num leading slashes from files.
1
3

Check out open source Scarab C++ library: https://github.com/loyso/Scarab

It does exactly what you described. It builds per-file diff using xdelta library and puts it to archive package. You can redistribute that package and apply the difference. There are binaries for Win32.

I'm the author of Scarab project.

2

Heads-up

Do not use -p0 unless you understand how patch works.


Diff

Create a deep (recursive) .diff between two directories in the same parent directory. A .diff file is a file that describes all the textual differences.

diff --unified --recursive --no-dereference ORIGINAL/ PATCHED/ > patch.diff
  • --unified: to format the output into a “unified context diff.”
  • --recursive: to create a deep .diff.
  • --no-dereference: not to follow symbolic links.

Although the directory names, ORIGINAL and PATCHED, are included in file paths in the output, patch.diff, they are not important (“won’t be used later on”).

Patch

With the patch.diff file, you can patch any directory of the same hierarchical structure. You don’t need the directories named ORIGINAL and PATCHED anymore.

For example, this command patches the directory_to_apply_the_patch_on/ directory according to the patch.diff.

patch --directory=directory_to_apply_the_patch_on/ --strip=1 < patch.diff
  • --directory: to set the working directory for patch.

patch assumes that file paths in patch.diff are relative to the working directory. Those paths have preceding PATCHED/, so you need to strip it first unless you have a PATCHED directory to patch in your working directory.

--strip=<N> or -p<N> strips the first N segments (delimited by slashes) from the file paths specified in the patch.diff file. --strip=1 strips the topmost directory PATCHED/ from all the destination file paths, making them relative to directory_to_apply_the_patch_on/ instead.

Vikram Dattu: is it possible to omit those outer directory names? Or is it neccessary that patch should contain new and orig directory names?

Flimm: Is it possible to apply the patch without requiring that directories named orig or new exist?

Yes, and that’s what --strip=1 is for.

For me, I prefer this:

patch --directory=directory_to_apply_the_patch_on/ --unified --strip=1 --posix --force --set-utc --verbose < patch.diff
  • --unified: to interpret the .diff file as a unified context diff, skipping format guessing.
  • --posix: to behave the POSIX-compliant way.
  • --force: not to ask questions on failure.
  • --set-utc: to update the modified times of patched files.
  • --verbose: to print what it’s thinking while trying to patch.
2
  • Interested to know what OS you use, as none of the diff options you use here are supported on macOS
    – David H
    Commented Mar 21, 2023 at 22:02
  • @DavidH I use Debian 12 and diff (GNU diffutils) 3.8. The options --unified[=NUM] and --recursive are also known as -u, -U NUM and -r. Mac has been supporting them since macOS was Mac OS X. Commented Mar 22, 2023 at 4:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.