brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: patch -R undoes a patch you applied

Linux
~/tips/tip-shell-patch-r-reverse-applied-patch

patch -R g.txt < f.patch

Reverses a patch, so the files go back to how they were before it was applied.

Applying a patch is the easy part. Taking it back out again is a thing I’d rather not do by hand, and patch has a switch for it.

patch -R g.txt < f.patch

-R (--reverse) tells patch to assume the patch was made with the old and new files swapped. In effect it applies the patch backwards. Here is a tiny round trip with a patch made by diff -u, where line two changes from two to 2:

patch g.txt < f.patch
patching file g.txt

patch -R g.txt < f.patch
patching file g.txt

After the first command the file said one 2 three, after the second one it was back to one two three.

Before reversing something bigger, combine it with --dry-run. In a test, patch -R --dry-run h.txt < f.patch printed checking file h.txt and changed nothing.

Two caveats. First, reversing only works while the file still looks like the patched version. If you edited the same lines afterwards, hunks can be rejected. Second, applying a patch that is already applied is not harmless either. When patch -N ran on a file that already had the change, it said Reversed (or previously applied) patch detected! Skipping patch. and also wrote a h.txt.rej file next to it, so look for stray .rej files afterwards.

If you want a safety net, -b keeps the original as k.txt.orig before patching.

Further reading: patch(1) manual page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link