patch — Apply a unified diff to source files, transforming them in place across all 5 shells
Equivalents in every shell
Bashunix
patch -p1 < changes.patchZshunix
patch -p1 < changes.patchFishunix
patch -p1 < changes.patchPowerShellwindows
git apply changes.patchNo native `patch.exe` on Windows. `git apply` reads the same unified-diff format and works from any Git install.
cmd.exewindows
git apply changes.patchSame 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.patchFish
patch -p1 < fix.patchPowerShell
git apply fix.patchcmd.exe
git apply fix.patchReverse an already-applied patch
Bash
patch -R -p1 < fix.patchPowerShell
git apply --reverse fix.patchDry-run: check whether a patch would apply cleanly
Bash
patch --dry-run -p1 < fix.patchPowerShell
git apply --check fix.patchGotchas
- `-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`.