20

Given a (source) patch file, what's the easiest way to apply this patch on the source files under Windows?

A GUI tool where I can visually compare the unchanged-changed source lines would be great.

Ashwin Nanjappa
  • 76,204
  • 83
  • 211
  • 292

4 Answers4

20

A good way to apply a patch file under Windows OS is using Git. As I understood, Git is a version control solution like SVN.

Here is a guideline to apply a patch :

  • First of all, download the latest release of the Windows Git Edition here : GIT
  • With the cmd prompt, change directory to the patch file and files to patch
  • Now you can use the following command line :
git apply --ignore-space-change --ignore-whitespace --whitespace=nowarn file.patch
Andreas Haferburg
  • 5,189
  • 3
  • 37
  • 63
Nicolas Thery
  • 2,319
  • 4
  • 26
  • 36
7

Not that since Git 2.3.3 (March 2015), you can use git apply --unsafe-paths to use git apply outside a git repo.

See commit 5244a31 by Junio C Hamano (gitster)

"git apply" was not very careful about reading from, removing, updating and creating paths outside the working tree (under --index/--cached) or the current directory (when used as a replacement for GNU patch).

The documentation now includes:

--unsafe-paths:

By default, a patch that affects outside the working area (either a Git controlled working tree, or the current working directory when "git apply" is used as a replacement of GNU patch) is rejected as a mistake (or a mischief).

When git apply is used as a "better GNU patch", the user can pass the --unsafe-paths option to override this safety check.
This option has no effect when --index or --cached is in use.

So if you have git installed, git apply could help, even outside of any git repo.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
6

Patch for Windows is what you're looking for.

SCdF
  • 57,260
  • 24
  • 77
  • 113
  • 9
    Note that this patch doesn't play nice with Vista's UAC. Try Git-for-windows's patch.exe instead, that comes with a manifest that solves the issue. – Macke Mar 08 '09 at 21:37
  • 1
    @Macke is right ! See my answer for a how-to with GIT ! – Nicolas Thery Jul 11 '15 at 14:39
  • Actually what @Macke is referring to is that in Windows an exec called `patch.exe` requires Admin access and if UAE is set to on, it pauses and prompts the user. Now, the Git version of `patch.exe` embeds a manifest that prevents the UAE elevation need. This happens to be a old legacy thing with windows in that any `exe` that has `install`, `patch`, `setup` in the name will force a UAE elevation UNLESS the manifest is supplied. The patch gets applied: [patch patch](https://github.com/msys2/MSYS2-packages/blob/900744becd072f687029b0f830ab6fe95cf533d6/patch/msys2-patch-manifest.patch) – Shawn Cicoria May 06 '20 at 00:56
-2

WinMerge is awesome.

http://winmerge.org/

Philip Reynolds
  • 9,364
  • 3
  • 30
  • 37
  • 2
    Winmerge is great at creating patches, but not at applying them. The OP asked about how to apply it. It would be a fabulous new feature if WinMerge could apply a patch and then show the changes that were made. – Warren P Oct 28 '11 at 20:05
  • 1
    **Found in the WinMerge Help** patches cannot be applied using WinMerge. However, you can apply patches using the patch tool from the GnuWin32 project, because WinMerge produces patch files that are compatible with GNU/diffutils. Also, see the preceding question. – Nicolas Thery Nov 07 '11 at 19:41
  • I was looking for this. Thanks. I did googling "how to make patches" this page come up top, so.. – the kamilz Dec 26 '19 at 08:19