How to undo after patch is applied? .rej/.orig files

diff()patch

The case is following. I have two files: file1.c, file2.c

ls 
file1.c file2.c patch.diff
patch < patch.diff
...
x out of x hunk FAILED -- saving rejects to file file1.c.rej 
(the same with file2.c)
ls
file1.c file2.c file1.c.orig file2.c.orig file1.c.rej file2.c.rej patch.diff
patch -R < patch.diff # I thought it will revert changes back
...
x out of x hunk FAILED -- saving rejects to file file1.c.rej
(again, the same with file2.c)    
ls
file1.c file2.c file1.c.orig file2.c.orig file1.c.rej file2.c.rej patch.diff

Now I left with broken files and have no idea how to get them back. It seems that the *.orig files were replaced on second pass with already broken changes. Any ideas?

Best Answer

It is always a good idea to make backup copies from the original files.

This can be done automatically if you call patch with option -b.

Background: in case that there is no .rej file, you could call:

patch -R

to reverse the patch, but this does not work in case of a problem.

Note that in case that file2.c.orig already exists when patch starts, this file is removed and replaced by a backup copy of the current state.

If you have these .orig files, you could easily rename them to the original filename in order to undo the patch.

Note that it may be a good idea to reverse all patches to all files in a project in case that a single patch fails. Since this requires .orig files for all patched files, it is recommended to use

patch -b

If you have these .orig files, you could call:

for i in *.orig; do
    base=`basename $i .orig`
    mv $i $base
done