【linux】patch命令使用简述

00. 开发环境

ubuntu1804
git : git version 2.17.1
patch : GNU patch 2.7.6

01. 命令简述

百度百科:https://baike.baidu.com/item/Patch/8198596?fromModule=lemma-qiyi_sense-lemma
patch可以将git diff 等命令生成的比较文件(patch文件)应用到不同版本的同一个工程中,是合并代码的一种方式。
其他功能:略

02. 工程结构

patch.git/
├── diff.patch
├── func.c
└── main.c

patch_br/
├── func.c
└── main.c

10. 实例

生成 patch

参考命令:git 、 diff 、xxx

# 假设将两次commit的修改合成为patch文件
# git diff xxx xxx > diff.patch
git diff 89ca304 cb36b55 > diff.patch

# 利用其它命令、格式生成patch文件
# git diff xxx > diff.patch

应用 patch

参考命令:patch

# 建议合并前先检查文件合法性
# --dry-run
patch -d ./patch_br/ --dry-run -p1 < ./patch.git/diff.patch
# 合并
# patch -d 工程目录 各类选项 -p剥离等级 < patch文件的地址
patch -d ./patch_br/ -p1 < ./patch.git/diff.patch

撤销 应用

# 模拟一下
patch -d ./patch_br/ --dry-run -R -p1 < ./patch.git/diff.patch
# 撤销已经应用的patch修改,reverse,即用旧的修改替换新的修改,实现修改的恢复操作
patch -d ./patch_br/ -R -p1 < ./patch.git/diff.patch

完整测试log

 # 新建一个.git仓库,用于生成patch文件
mkdir patch.git
cd patch.git/
touch main.c
touch func.c
git init .
git add *.c
git commit -m "Codebase"

# 新建另一个测试工程(将git仓库的文件拷贝过去,git clone 也可以)
cd ../
mkdir patch_br
cp ./patch.git/*.c ./patch_br/

# 修改仓库的文件,并修改、提交,最后生成patch文件
cd patch.git/
echo update >> main.c
echo update >> func.c
git add -u .
git commit -m "Update"
git log --oneline --decorate --graph  --all
* cb36b55 (HEAD -> master) Update
* 89ca304 Codebase
git diff 89ca304 cb36b55 > diff.patch  、// 生成patch文件

# 应用patch,指定工程路径、patch文件路径等参数
cd ../
patch -d ./patch_br/ --dry-run -p1 < ./patch.git/diff.patch   // 执行合并前先检查各项文件等是否合法
checking file func.c
checking file main.c
patch -d ./patch_br/ -p1 < ./patch.git/diff.patch	// 正式应用patch
patching file func.c
patching file main.c

# 撤销
patch -d ./patch_br/ --dry-run -R -p1 < ./patch.git/diff.patch
patch -d ./patch_br/ -R -p1 < ./patch.git/diff.patch

80. patch 各类参数简述

  1. 简述:
-pN			: 剥离等级,移除patch文件的具体文件的路径个数,用于使patch文件中的修改能与指定的被修改的文件路径匹配
-B 			:自动备份源文件
--dryrun	: 模拟运行,可以检查是否有错误
-d			: 指定需要应用patch的文件路径(与-p要配合好)
-R			: 撤销/恢复已经应用的patch(将patch文件中新的修改与旧的修改反向应用到工程中,达到代码恢复的目的)
  1. 帮助信息:
Usage: patch [OPTION]... [ORIGFILE [PATCHFILE]]

Input options:

  -p NUM  --strip=NUM  Strip NUM leading components from file names.
  -F LINES  --fuzz LINES  Set the fuzz factor to LINES for inexact matching.
  -l  --ignore-whitespace  Ignore white space changes between patch and input.

  -c  --context  Interpret the patch as a context difference.
  -e  --ed  Interpret the patch as an ed script.
  -n  --normal  Interpret the patch as a normal difference.
  -u  --unified  Interpret the patch as a unified difference.

  -N  --forward  Ignore patches that appear to be reversed or already applied.
  -R  --reverse  Assume patches were created with old and new files swapped.

  -i PATCHFILE  --input=PATCHFILE  Read patch from PATCHFILE instead of stdin.

Output options:

  -o FILE  --output=FILE  Output patched files to FILE.
  -r FILE  --reject-file=FILE  Output rejects to FILE.

  -D NAME  --ifdef=NAME  Make merged if-then-else output using NAME.
  --merge  Merge using conflict markers instead of creating reject files.
  -E  --remove-empty-files  Remove output files that are empty after patching.

  -Z  --set-utc  Set times of patched files, assuming diff uses UTC (GMT).
  -T  --set-time  Likewise, assuming local time.

  --quoting-style=WORD   output file names using quoting style WORD.
    Valid WORDs are: literal, shell, shell-always, c, escape.
    Default is taken from QUOTING_STYLE env variable, or 'shell' if unset.

Backup and version control options:

  -b  --backup  Back up the original contents of each file.
  --backup-if-mismatch  Back up if the patch does not match exactly.
  --no-backup-if-mismatch  Back up mismatches only if otherwise requested.

  -V STYLE  --version-control=STYLE  Use STYLE version control.
    STYLE is either 'simple', 'numbered', or 'existing'.
  -B PREFIX  --prefix=PREFIX  Prepend PREFIX to backup file names.
  -Y PREFIX  --basename-prefix=PREFIX  Prepend PREFIX to backup file basenames.
  -z SUFFIX  --suffix=SUFFIX  Append SUFFIX to backup file names.

  -g NUM  --get=NUM  Get files from RCS etc. if positive; ask if negative.
  -g NUM  --get=NUM  Get files from RCS etc. if positive; ask if negative.

Miscellaneous options:

  -t  --batch  Ask no questions; skip bad-Prereq patches; assume reversed.
  -f  --force  Like -t, but ignore bad-Prereq patches, and assume unreversed.
  -s  --quiet  --silent  Work silently unless an error occurs.
  --verbose  Output extra information about the work being done.
  --dry-run  Do not actually change any files; just print what would happen.
  --posix  Conform to the POSIX standard.

  -d DIR  --directory=DIR  Change the working directory to DIR first.
  --reject-format=FORMAT  Create 'context' or 'unified' rejects.
  --binary  Read and write data in binary mode.
  --read-only=BEHAVIOR  How to handle read-only input files: 'ignore' that they
                        are read-only, 'warn' (default), or 'fail'.

  -v  --version  Output version info.
  --help  Output this help.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

过得精彩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值