paulgorman.org/technical

diff and patch

(2019)

See diff(1) and patch(1).

🐚 $ cat original_file
Line one: red.
Line two: blue.
Line three: green.
Line four: A.
Line five: B.
Line six: C.
Line seven: D.
🐚 $ cat changed_file
Line one: red.
Line two: blue.
Line three: green.
Added line: a new line!
Line four: A.
Line five: B.
Line six: changed to not C.
Line seven: D.
🐚 $ diff original_file changed_file 
3a4
> Added line: a new line!
6c7
< Line six: C.
---
> Line six: changed to not C.
🐚 $ diff -u original_file changed_file
--- original_file       2019-07-11 17:00:37.462301169 -0400
+++ changed_file        2019-07-11 17:01:53.522577725 -0400
@@ -1,7 +1,8 @@
 Line one: red.
 Line two: blue.
 Line three: green.
+Added line: a new line!
 Line four: A.
 Line five: B.
-Line six: C.
+Line six: changed to not C.
 Line seven: D.
🐚 $ diff -u original_file changed_file > my.patch
🐚 $ mv changed_file /tmp/
🐚 $ cp original_file /tmp/
🐚 $ patch < my.patch
patching file original_file
🐚 $ cat original_file
Line one: red.
Line two: blue.
Line three: green.
Added line: a new line!
Line four: A.
Line five: B.
Line six: changed to not C.
Line seven: D.

Reverse an already applied patch:

🐚 $ patch -R < my.patch 
patching file original_file
🐚 $ cat original_file 
Line one: red.
Line two: blue.
Line three: green.
Line four: A.
Line five: B.
Line six: C.
Line seven: D.

Ansible has a patch module.

https://docs.ansible.com/ansible/latest/modules/patch_module.html

- name: Apply patch to one file
  patch:
    src: /tmp/index.html.patch
    dest: /var/www/index.html