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

11,705

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
Share:
11,705

Related videos on Youtube

Timur Fayzrakhmanov
Author by

Timur Fayzrakhmanov

Updated on September 18, 2022

Comments

  • Timur Fayzrakhmanov
    Timur Fayzrakhmanov over 1 year

    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?

    • roaima
      roaima almost 6 years
      Are your two x out of x hunk FAILED messages the same? Are the values of x the same? It would help if you could put proper messages here - it doesn't affect your security.
    • phemmer
      phemmer almost 6 years
      There's nothing wrong with asking this question here, but since the question is about software development, you'll likely get much better answers on stackoverflow.
  • Timur Fayzrakhmanov
    Timur Fayzrakhmanov almost 6 years
    Does it backup (with -b flag) even if the same filename already exists?
  • schily
    schily almost 6 years
    What do you understand by "the same filename"?
  • schily
    schily almost 6 years
    It is not granted behavior that patch created a .orig file in case of a failure. BTW: .bak was a typo