Skip to content
shellmap

patchApply a unified diff to source files, transforming them in place across all 5 shells

Equivalents in every shell

Bashunix
patch -p1 < changes.patch
Zshunix
patch -p1 < changes.patch
Fishunix
patch -p1 < changes.patch
PowerShellwindows
git apply changes.patch

No native `patch.exe` on Windows. `git apply` reads the same unified-diff format and works from any Git install.

cmd.exewindows
git apply changes.patch

Same as PowerShell — use Git for Windows or GnuWin32 patch. Plain cmd.exe has no equivalent.

Worked examples

Apply a patch produced by `git diff` or `diff -u`

Bash
patch -p1 < fix.patch
Fish
patch -p1 < fix.patch
PowerShell
git apply fix.patch
cmd.exe
git apply fix.patch

Reverse an already-applied patch

Bash
patch -R -p1 < fix.patch
PowerShell
git apply --reverse fix.patch

Dry-run: check whether a patch would apply cleanly

Bash
patch --dry-run -p1 < fix.patch
PowerShell
git apply --check fix.patch

Gotchas

  • `-p<N>` strips the first N path components from filenames in the patch header. `-p1` is correct for diffs produced by `git diff` or `diff -ru a/old b/new`; `-p0` is correct for diffs produced without an `a/` / `b/` prefix.
  • When a patch fails partway, `patch` leaves `.rej` and `.orig` files behind. `git apply` is all-or-nothing — either every hunk applies or none do.
  • Patches generated with `diff -uN` (no-prefix) need `-p0`. Patches from `git format-patch` need `-p1`. Mixing them up is the #1 source of "malformed patch" errors.
  • Windows ships no `patch.exe`. Git for Windows includes one in `usr/bin/patch.exe`, but for new scripts prefer `git apply` since it is guaranteed to exist wherever Git does.
  • Fish has no shell-builtin redirect syntax difference from bash for `<`, so `patch -p1 < file.patch` works unchanged.

WSL & PowerShell Core notes

pwshPowerShell Core has no `patch` cmdlet. On Linux/macOS pwsh the system `patch` binary works. On Windows pwsh, install Git for Windows and call `git apply` (or invoke the bundled `C:\Program Files\Git\usr\bin\patch.exe` directly).
WSLWSL ships GNU patch. Applying a patch that was generated on Windows (CRLF line endings) against Linux sources (LF) often produces "malformed patch" — normalize with `dos2unix file.patch` or use `git apply --ignore-whitespace`.

Related commands