How do I apply a diff patch on Windows?

WindowsDiffGnuPatch

Windows Problem Overview


There are plenty of programs out there that can create a diff patch, but I'm having a heck of a time trying to apply one. I'm trying to distribute a patch, and I got a question from a user about how to apply it. So I tried to figure it out on my own and found out that I have no clue, and most of the tools I can find are command-line. (I can handle a command line, but a lot of people would be lost without a nice, friendly GUI. So those are no good for this purpose.)

I tried using TortoiseSVN. I have the patch I'd like to apply. I right-click on the patch, and there's an option under the TortoiseSVN submenu that says "Apply patch." All it does is pull up an empty window.

So I tried hitting Open. It has two options: merge and apply unified diff. (The patch is in unified diff format, luckily.) But the apply option just plain doesn't work: It asks for the patch and a folder. Somehow it forgot to ask for the file to apply the patch to! So TortoiseSVN just plain doesn't work. Is there a Windows GUI-based utility that will take a patch and a file and apply it properly?

EDIT: Looking at the replies so far, it seems that Tortoise will only do it right if it's a file that's already versioned. That's not the case here. I need to be able to apply a patch to a file that did not come out of an SVN repository. I just tried using Tortoise, because I happen to know that SVN uses diffs and has to know how to both create them and apply them.

Windows Solutions


Solution 1 - Windows

Apply Patch

With TortoiseMerge:

  1. Find and open an existing SVN repo directory
  2. Create a new directory named "merges", if it does not exist already
  3. Copy the file onto which you want to apply the .patch file
  4. ADD and COMMIT to the svn repository before you continue to the next step
  5. Right click on merges and choose Apply patch...
  6. Double click the file from list
  7. The patched file with diff is displayed on the right pane
  8. Click on that pane and hit Save or export with File->Save As...

Alternative screeny if you Open from TortoiseMerge. In the screeny below, directory refers to the "merges" directory mentioned at step 2 above: Screeny

Screenshot of WinMerge GUI: Screeny

Solution 2 - Windows

I made pure Python tool just for that. It has predictable cross-platform behavior. Although it doesn't create new files (at the time of writing this) and lacks a GUI, it can be used as a library to create graphic tool.

UPDATE: It should be more convenient to use it if you have Python installed.

pip install patch
python -m patch

Solution 3 - Windows

TortoiseMerge is a separate utility that comes bundled with TortoiseSVN.

It can also be can be downloaded separately in the TortoiseDiff.zip archive. This will allow you to apply unified diffs to non-versioned files.

Solution 4 - Windows

The patch.exe utility from the Git installation works on Windows 10.

Install Git for Windows then use the "C:\Program Files\Git\usr\bin\patch.exe" command to apply a patch.

If any error message like a Hunk #1 FAILED at 1 (different line endings). had been got on the output during applying a patch, try to add the -l (that is a shortcut for the --ignore-whitespace) or the --binary switches to the command line.

Solution 5 - Windows

I know you said you would prefer a GUI, but the commandline tools will do the work nicely. See GnuWin for a port of unix tools to Windows. You'd need the patch command, obviously ;-)

You might run into a problem with the line termination, though. The GnuWin port will assume that the patchfile has DOS style line termination (CR/LF). Try to open the patchfile in a reasonably smart editor and it will convert it for you.

Solution 6 - Windows

In TortoiseSVN, patch applying does work. You need to apply the patch to the same directory as it was created from. It is always important to keep this in mind. So here's how you do it in TortoiseSVN:

Right click on the folder you want to apply the patch to. It will present a dialog asking for the location of the patch file. Select the file and this should open up a little file list window that lists the changed files, and clicking each item should open a diff window that shows what the patch is about to do to that file.

Good luck.

Solution 7 - Windows

You can use this Win32 native port of the patch utility.

It comes along with a bigger selection of other utilities and in contrast to Cygwin and similar it does not need any DLLs or similar. Just pick your tiny executable of choice and store it wherever you want.

Simple usage:

patch.exe -i <patchfile>

Get more help:

patch.exe --help

Solution 8 - Windows

I use MSYS2 from http://www.msys2.org/

It provides many utilities like patch, which, git, tree, and many more.

After installing MSYS2 simply run the package manager to install patch:

pacman -S patch

Solution 9 - Windows

> EDIT: Looking at the replies so far, it seems that Tortoise will only do it right if it's a file that's already versioned. That's not the case here. I need to be able to apply a patch to a file that did not come out of an SVN repository. I just tried using Tortoise because I happen to know that SVN uses diffs and has to know how to both create them and apply them.

You can install Cygwin, then use the command-line patch tool to apply the patch. See also this Unix man page, which applies to patch.

Solution 10 - Windows

It appears that TortoiseSVN (TortoiseMerge) requires the line Index: foobar.py in the diff/patch file. This is what I needed to do to make a non-TortoiseSVN patch file work with TortoiseSVN's right-click Apply Patch command.

Before:

--- foobar.py.org	Sat May 08 16:00:56 2010
+++ foobar.py	Sat May 08 15:47:48 2010

After:

Index: foobar.py
===================================================================
--- foobar.py
+++ foobar.py	(working copy)

Or if you know the specific revision your contributor was working from:

Index: foobar.py
===================================================================
--- foobar.py	(revision 1157)
+++ foobar.py	(working copy)

Solution 11 - Windows

I am already using BeyondCompare (commercial) for diffs and merges, and this tool also has the capability to create, view and apply patches.

Solution 12 - Windows

The patch tells it what file to apply to. The header should be something like (view it in Notepad or your fav text editor):

--- Folder/old_file
+++ Folder/new_file

In the case of a Subversion patch, you'd have revision numbers as well (since the file names are the same).

GNU patch will let you override those names, but I don't know of any GUI tools to do the same. I'd check with the various diff programs - though, it does not appear WinMerge supports applying patches.

Solution 13 - Windows

If you are using Mercurial, this is done via "import". So at the command line, the hg import command, or (you may find the --no-commit option useful), or "Repository" => "Import..." in Hg Workbench.

Note that these will commit the changes by default; you can avoid this using hg import --no-commit option if using the command-line, or if you used Hg Workbench, you might find it useful to issue hg rollback after the merge.

Solution 14 - Windows

Eclipse should be able to do it, go to TeamSynchronize perspective and then into Project->Apply patch

Solution 15 - Windows

For Java projects, I have used NetBeans to apply patch files. If the Java code you are patching is not already a NetBeans project, create a project for it. To create a new project:

  • Select menu File -> New Project
  • In the resulting dialog, make it a Java Application project. Give it a name in the dialog, and click Finish.
  • Right-click the name of your project, and select Properties from the context menu
  • In the resulting dialog, select Sources, and add a Source Folder. Browse to your Java source.

Now that you have a project, apply the patch:

  • Highlight your project to select it
  • From the main menu, select menu Tools -> Apply Diff Patch
  • In the resulting dialog, browse to your patch file, select it, and press the Patch button.

That's it. Your patch should be applied, and you should see a diff window showing the changes.

Solution 16 - Windows

Using git Diff or linux patch to apply a patch on windows using git diff

Patches created anywhere on linux, MacOS or else, using the GNU patch command or git diff can be all applied on windows using git apply.

Create the patch

For instance to create the patch from 2 directories in which 1 or more files have been changed:

diff -Naru original_dir modified_dir > 0001-path-file.patch

  • "-N" treat absent files as empty, necessary to only consider modified content
  • "-a" treat all files as text, not compulsory, needs removed if dealing - with binary patches.
  • "-r" recursive for directories traversal
  • "-u" adds 3 lines of context by default around all diffs

OR using git diff

git diff original_dir modified_dir > 0001-path-file.patch

Then copy the .patch file on the windows environment as well as the original_dir directory that contains the original unchanged files.

Apply with git diff:

1 Copy the patch file in the parent directory

2 cd into the original directory

3 Apply the patch using git apply

copy 0001-path-file.patch ..\original_dir\
cd original_dir
git apply < 0001-path-file.patch

Solution 17 - Windows

When applying patches using TortoiseSVN, I typically save the path in the root of the checked out repository. You should then be able to right click on the patch, go to the TortoiseSVN menu, and click ApplyPatch. ApplyPatch should automatically figure out which level in the directory hierarchy the patch was created.

I have, however, had issues in the past with applying patches that contain new files, or which involve renames to files. Whatever algorithm Tortoise uses for this doesn't seem to handle those scenarios very well. Unicode can give you similar issues.

Solution 18 - Windows

Do you have two monitors? I was having the same issue with TortoiseMerge and I realized that when I disabled one of the monitors the little window with the file list showed up. Hope this helps you.

Solution 19 - Windows

If you are getting "Not a working copy" error message then try selecting a directory from TortoiseMerge dialog box which is a working directory of SVN.

Solution 20 - Windows

A BusyBox port for Windows has both a diff and patch command, but they only support unified format.

Solution 21 - Windows

Just use:

patch -p0 < path-file.patch

remember execute this command only from the folder location where you created the patch.

Solution 22 - Windows

If you have git install on Windows and you want to apply a patch for a git repository, you can simply do from a Windows Power Shell:

git apply ..\0001-your-patch.patch

Solution 23 - Windows

in response to the query earlier about patch and Python 3, I got the error TypeError: can only concatenate str (not "bytes") to str

The solution for me was to change the source, in the _normalize_filenames function:

-        debug("    target = " + p.target)
-        debug("    source = " + p.source)
+        debug("    target = ", p.target)
+        debug("    source = ", p.source)

then patch worked fine for me (Python 3, Windows 10)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMason WheelerView Question on Stackoverflow
Solution 1 - WindowsSheriff MdView Answer on Stackoverflow
Solution 2 - Windowsanatoly techtonikView Answer on Stackoverflow
Solution 3 - WindowsPaul ShannonView Answer on Stackoverflow
Solution 4 - WindowsEvgeniy GeneralovView Answer on Stackoverflow
Solution 5 - WindowsSardaukarView Answer on Stackoverflow
Solution 6 - WindowsDanView Answer on Stackoverflow
Solution 7 - WindowslogischView Answer on Stackoverflow
Solution 8 - WindowsisapirView Answer on Stackoverflow
Solution 9 - WindowsBrian ClapperView Answer on Stackoverflow
Solution 10 - Windowsmatt wilkieView Answer on Stackoverflow
Solution 11 - WindowsFourtyTwoView Answer on Stackoverflow
Solution 12 - WindowsMark BrackettView Answer on Stackoverflow
Solution 13 - WindowsMarc GravellView Answer on Stackoverflow
Solution 14 - WindowsJose OspinaView Answer on Stackoverflow
Solution 15 - Windowsuser1984699View Answer on Stackoverflow
Solution 16 - WindowsrsacchettiniView Answer on Stackoverflow
Solution 17 - WindowstsellonView Answer on Stackoverflow
Solution 18 - WindowsBrunoView Answer on Stackoverflow
Solution 19 - WindowsVinod DalviView Answer on Stackoverflow
Solution 20 - WindowsMeowView Answer on Stackoverflow
Solution 21 - WindowsAshish LohiaView Answer on Stackoverflow
Solution 22 - WindowsOlivierMView Answer on Stackoverflow
Solution 23 - WindowsAlasdair YatesView Answer on Stackoverflow