原生文本:
# a.txt
abcdefgh
12345678
# b.txt
abc123def
abcdefgh
0123456789
hello
使用 diff
命令:
jiaming@jiaming-VirtualBox:~/Documents$ diff a.txt b.txt
0a1
> abc123def
3c4,6
< 12345678
---
> 0123456789
>
> hello
diff -Naur
命令生成 patch
所需要的 .diff
文件:
jiaming@jiaming-VirtualBox:~/Documents$ diff -Naur a.txt b.txt > c.diff # 在 a.txt 基础上做了 c.diff 中的改变,变成了 b.txt
jiaming@jiaming-VirtualBox:~/Documents$ cat c.diff
--- a.txt 2021-02-10 14:22:22.601177980 +0800
+++ b.txt 2021-02-10 14:25:44.578127476 +0800
@@ -1,3 +1,6 @@
+abc123def
abcdefgh
-12345678
+0123456789
+
+hello
打补丁:
jiaming@jiaming-VirtualBox:~/Documents$ patch < c.diff # 对基础 a.txt 做 c.diff 改变,最终结果变为 b.txt **cat c.diff > patch**
patching file a.txt
jiaming@jiaming-VirtualBox:~/Documents$ cat a.txt
abc123def
abcdefgh
0123456789
hello
jiaming@jiaming-VirtualBox:~/Documents$ cat b.txt
abc123def
abcdefgh
0123456789
hello
撤销补丁:
jiaming@jiaming-VirtualBox:~/Documents$ patch -R < c.diff
patching file a.txt
jiaming@jiaming-VirtualBox:~/Documents$ cat a.txt
abcdefgh
12345678
jiaming@jiaming-VirtualBox:~/Documents$ cat b.txt
abc123def
abcdefgh
0123456789
hello
jiaming@jiaming-VirtualBox:~/Documents$ diff b.txt a.txt
1d0
< abc123def
4,6c3
< 0123456789
<
< hello
---
> 12345678
jiaming@jiaming-VirtualBox:~/Documents$ diff -Naur b.txt a.txt > d.diff
jiaming@jiaming-VirtualBox:~/Documents$ cat d.diff
--- b.txt 2021-02-10 14:25:44.578127476 +0800
+++ a.txt 2021-02-10 14:22:22.601177980 +0800
@@ -1,6 +1,3 @@
-abc123def
abcdefgh
-0123456789
-
-hello
+12345678