一. 准备工作:

[root@guangzhou gittest]# git br
* master
[root@guangzhou gittest]# git chk -b patch-test1 && git chk -b patch-test2
切换到一个新分支 'patch-test1'
切换到一个新分支 'patch-test2'
[root@guangzhou gittest]# git br
master
patch-test1
* patch-test2
#当前提交记录
[root@guangzhou gittest]# git log
commit e92a420301cf7dffccbfc1c3830bbdd3234a25dd
Author: carl <xxxx@qq.com>
Date: Wed Oct 6 08:39:14 2021 +0000 Initial commit
[root@guangzhou gittest]#

新增commit记录

[root@guangzhou gittest]# echo "111" >log.txt
[root@guangzhou gittest]# git add .
[root@guangzhou gittest]# git commit -m 'log.txt add 111'
[patch-test2 4daba4c] log.txt add 111
1 file changed, 1 insertion(+)
create mode 100644 log.txt
[root@guangzhou gittest]# echo "222" >log.txt
[root@guangzhou gittest]# git add .
[root@guangzhou gittest]# git commit -m 'log.txt add 222'
[patch-test2 d585699] log.txt add 222
1 file changed, 1 insertion(+), 1 deletion(-)
[root@guangzhou gittest]# echo "333" >> test.txt
[root@guangzhou gittest]# git add .
[root@guangzhou gittest]# git commit -m 'test.txt add 333'
[patch-test2 43e11e9] test.txt add 333
1 file changed, 1 insertion(+)
create mode 100644 test.txt
[root@guangzhou gittest]# echo "444" >> test.txt
[root@guangzhou gittest]# git add .
[root@guangzhou gittest]# git commit -m 'test.txt add 444'
[patch-test2 a0d9657] test.txt add 444
[root@guangzhou gittest]# git add .
[root@guangzhou gittest]# git commit -m 'ceshi.txt add 666'
[patch-test2 164aaab] ceshi.txt add 666
1 file changed, 1 insertion(+)
[root@guangzhou gittest]# echo "777" >> final.txt
[root@guangzhou gittest]# git add .
[root@guangzhou gittest]# git commit -m 'final.txt add 777'
[patch-test2 9131774] final.txt add 777
1 file changed, 1 insertion(+)
create mode 100644 final.txt

打印新增记录

[root@guangzhou gittest]# git log
commit 91317743d8d805910a835c4bf7169aad8ffa5810
Author: carl <xxxx@qq.com>
Date: Thu Oct 7 10:25:43 2021 +0800 final.txt add 777 commit 164aaab5a85d79e41997202e7b528ff17557135b
Author: carl <xxxx@qq.com>
Date: Thu Oct 7 10:22:32 2021 +0800 ceshi.txt add 666 commit 09853f62cdd85d18cecc195d8a7f2e3c9693e7fc
Author: carl <xxxx@qq.com>
Date: Thu Oct 7 10:21:59 2021 +0800 ceshi.txt add 555 commit a0d9657d5bdcf04530bd16a2d08bbb58135ba10a
Author: carl <xxxx@qq.com>
Date: Thu Oct 7 10:21:38 2021 +0800 test.txt add 444 commit 43e11e9a983c9c5e5c8735fb94a3c567cb4a80e2
Author: carl <xxxx@qq.com>
Date: Thu Oct 7 10:21:26 2021 +0800 test.txt add 333 commit d585699ad87c07ed0e52932297ca37b64866e4e8
Author: carl <xxxx@qq.com>
Date: Thu Oct 7 10:20:45 2021 +0800 log.txt add 222 commit 4daba4ce9415508b579330be2bdde9e5c0c2dc40
Author: carl <xxxx@qq.com>
Date: Thu Oct 7 10:20:23 2021 +0800 log.txt add 111 commit e92a420301cf7dffccbfc1c3830bbdd3234a25dd
Author: carl <xxxx@qq.com>
Date: Wed Oct 6 08:39:14 2021 +0000 Initial commit
[root@guangzhou gittest]#

二. 使用diff (git diff startCommitId endCommitId > /tmp/xxx.diff):

[root@guangzhou gittest]# git diff e92a420301cf7dffccbfc1c3830bbdd3234a25dd d585699ad87c07ed0e52932297ca37b64866e4e8 > /tmp/patch-diff.diff

[root@guangzhou gittest]# git chk patch-test1
切换到分支 'patch-test1'
[root@guangzhou gittest]# git apply --stat /tmp/patch-diff.diff
log.txt | 1 +
1 file changed, 1 insertion(+)
[root@guangzhou gittest]# git apply --check /tmp/patch-diff.diff
[root@guangzhou gittest]# git apply /tmp/patch-diff.diff
[root@guangzhou gittest]# git ss
# 位于分支 patch-test1
# 未跟踪的文件:
# (使用 "git add <file>..." 以包含要提交的内容)
#
# log.txt
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@guangzhou gittest]# git add .
[root@guangzhou gittest]# git commit -m 'patch-test2 log.txt commit'
[patch-test1 5d07cdf] patch-test2 log.txt commit
1 file changed, 1 insertion(+)
create mode 100644 log.txt
[root@guangzhou gittest]# git log
commit 5d07cdf186f5a4bba16fcf36aff02d7cdbf9338f
Author: carl <xxxx@qq.com>
Date: Thu Oct 7 10:45:59 2021 +0800 patch-test2 log.txt commit commit e92a420301cf7dffccbfc1c3830bbdd3234a25dd
Author: carl <xxxx@qq.com>
Date: Wed Oct 6 08:39:14 2021 +0000 Initial commit
[root@guangzhou gittest]# ls
log.txt README.en.md README.md
[root@guangzhou gittest]# cat log.txt
222

以上可见,patch-test2分支创建的补丁文件/tmp/patch-diff.diff已被成功应用到patch-test1分支。

不过commit信息已丢失。

三. 使用apply:

  git format-patch startCommitId...endCommitId -o /tmp/xxx.patch #生成patch

  git apply --stat  /tmp/xxx.patch #检测path是否可用

  git apply --check /tmp/xxx.patch #检测patch是否可用

  git apply /tmp/xxx.patch #使用patch文件

[root@guangzhou gittest]# git format-patch d585699ad87c07ed0e52932297ca37b64866e4e8...a0d9657d5bdcf04530bd16a2d08bbb58135ba10a -o /tmp/patch-test.patch
/tmp/patch-test.patch/0001-test.txt-add-333.patch
/tmp/patch-test.patch/0002-test.txt-add-444.patch #--stat和--check检测补丁文件是否可用
[root@guangzhou gittest]# git apply --stat /tmp/patch-test.patch/0001-test.txt-add-333.patch && git apply --check /tmp/patch-test.patch/0001-test.txt-add-333.patch && git apply /tmp/patch-test.patch/0001-test.txt-add-333.patch
test.txt | 1 +
1 file changed, 1 insertion(+)
[root@guangzhou gittest]# git apply --stat /tmp/patch-test.patch/0002-test.txt-add-444.patch && git apply --check /tmp/patch-test.patch/0002-test.txt-add-444.patch && git apply /tmp/patch-test.patch/0002-test.txt-add-444.patch
test.txt | 1 +
1 file changed, 1 insertion(+)
[root@guangzhou gittest]# git ss
# 位于分支 patch-test1
# 未跟踪的文件:
# (使用 "git add <file>..." 以包含要提交的内容)
#
# test.txt
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@guangzhou gittest]# cat test.txt
333
444
[root@guangzhou gittest]# git add .
[root@guangzhou gittest]# git commit -m 'patch-test2 test.txt 333-444'
[patch-test1 8fd4e22] patch-test2 test.txt 333-444
1 file changed, 2 insertions(+)
create mode 100644 test.txt
[root@guangzhou gittest]# git log
commit 8fd4e222eaa2861653e3b5526b87e165380a3202
Author: carl <xxxx@qq.com>
Date: Thu Oct 7 10:57:27 2021 +0800 patch-test2 test.txt 333-444 commit 5d07cdf186f5a4bba16fcf36aff02d7cdbf9338f
Author: carl <xxxx@qq.com>
Date: Thu Oct 7 10:45:59 2021 +0800 patch-test2 log.txt commit commit e92a420301cf7dffccbfc1c3830bbdd3234a25dd
Author: carl <xxxx@qq.com>
Date: Wed Oct 6 08:39:14 2021 +0000 Initial commit
[root@guangzhou gittest]#

以上可见,通过apply的补丁文件已成功加入patch-test1分支,同diff一样,commit信息已丢失。

四. 使用am:

  git format-patch startCommitId...endCommitId -o /tmp/xxx.patch #生成patch

  git apply --stat  /tmp/xxx.patch #检测path是否可用

  git apply --check /tmp/xxx.patch #检测patch是否可用

  git am /tmp/xxx.patch #使用patch文件

[root@guangzhou gittest]# git format-patch a0d9657d5bdcf04530bd16a2d08bbb58135ba10a...164aaab5a85d79e41997202e7b528ff17557135b -o /tmp/patch-test2.patch
/tmp/patch-test2.patch/0001-ceshi.txt-add-555.patch
/tmp/patch-test2.patch/0002-ceshi.txt-add-666.patch
[root@guangzhou gittest]# git chk patch-test1
切换到分支 'patch-test1'
[root@guangzhou gittest]# git apply --stat /tmp/patch-test2.patch/0001-ceshi.txt-add-555.patch && git apply --check /tmp/patch-test2.patch/0001-ceshi.txt-add-555.patch
ceshi.txt | 1 +
1 file changed, 1 insertion(+) #这里会报错,需要把已经check的patch使用上
[root@guangzhou gittest]# git apply --stat /tmp/patch-test2.patch/0002-ceshi.txt-add-666.patch && git apply --check /tmp/patch-test2.patch/0002-ceshi.txt-add-666.patch
ceshi.txt | 1 +
1 file changed, 1 insertion(+)
error: ceshi.txt:?????????
[root@guangzhou gittest]# git am /tmp/patch-test2.patch/0001-ceshi.txt-add-555.patch
正应用:ceshi.txt add 555
[root@guangzhou gittest]# git apply --stat /tmp/patch-test2.patch/0002-ceshi.txt-add-666.patch && git apply --check /tmp/patch-test2.patch/0002-ceshi.txt-add-666.patch
ceshi.txt | 1 +
1 file changed, 1 insertion(+)
[root@guangzhou gittest]# git am /tmp/patch-test2.patch/0002-ceshi.txt-add-666.patch
正应用:ceshi.txt add 666
[root@guangzhou gittest]# git ss
# 位于分支 patch-test1
无文件要提交,干净的工作区
[root@guangzhou gittest]# git log
commit 7f6f77e592f5647d870b165baa79862b289b9b88
Author: carl <xxxx@qq.com>
Date: Thu Oct 7 10:22:32 2021 +0800 ceshi.txt add 666 commit 2acdcd883c856a3947721138b42e731b744ece94
Author: carl <xxxx@qq.com>
Date: Thu Oct 7 10:21:59 2021 +0800 ceshi.txt add 555 commit 8fd4e222eaa2861653e3b5526b87e165380a3202
Author: carl <xxxx@qq.com>
Date: Thu Oct 7 10:57:27 2021 +0800 patch-test2 test.txt 333-444 commit 5d07cdf186f5a4bba16fcf36aff02d7cdbf9338f
Author: carl <xxxx@qq.com>
Date: Thu Oct 7 10:45:59 2021 +0800 patch-test2 log.txt commit commit e92a420301cf7dffccbfc1c3830bbdd3234a25dd
Author: carl <xxxx@qq.com>
Date: Wed Oct 6 08:39:14 2021 +0000 Initial commit
[root@guangzhou gittest]# cat ceshi.txt
555
666
[root@guangzhou gittest]# ls
ceshi.txt log.txt README.en.md README.md test.txt
[root@guangzhou gittest]#

以上可见通过am已成功把补丁文件应用,并且保留了commit信息。

GIT打补丁 - patch和diff应用的更多相关文章

  1. 关于git的打patch的功能

    UNIX世界的软件开发大多都是协作式的,因此,Patch(补丁)是一个相当重要的东西,因为几乎所有的大型UNIX项目的普通贡献者,都是通过 Patch来提交代码的.作为最重要的开源项目之一,Linux ...

  2. 补丁(patch)的制作与应用

    命令简介 用到的两个命令是diff和patch. diff diff可以比较两个东西,并可同时记录下二者的区别.制作补丁时的一般用法和常见选项为: diff [选项] 源文件(夹) 目的文件(夹) - ...

  3. Git打补丁常见问题

    Git打补丁常见问题 往往觉得得到某个功能的补丁就觉得这个功能我就已经成功拥有了,可是在最后一步的打补丁的工作也是须要相当慎重的,甚至有可能还要比你获取这个补丁花费的时间还要多.看到好多同行遇到这个问 ...

  4. 内核诊断(二)-- patch 和diff

    patch文件结构 生成patch文件 --diff命令 patch 使用 -- patch命令 3.1 打path 3.1撤销patch 使用举例 4.1 基本命令使用 4.2 内核打补丁 1. p ...

  5. 打补丁patch 命令使用

    打补丁patch 命令使用 http://www.cnblogs.com/huanghuang/archive/2011/07/14/2106402.html patch 命令用于打补丁,补丁文件是使 ...

  6. patch与diff的恩怨

    一.概述 diff和patch是一对相辅相成的工具,在数学上来说,diff类似于对两个集合的差运算,patch类似于对两个集合的和运算.diff比较两个文件或文件集合的差异,并记录下来,生成一个dif ...

  7. 如何用git命令生成Patch和打Patch

    在程序员的日常开发与合作过程中,对于code的生成patch和打patch(应用patch)成为经常需要做的事情.什么是patch?简单来讲,patch中存储的是你对代码的修改,生成patch就是记录 ...

  8. 找不同diff-打补丁patch

    Q:为什么要找不同,为什么要打补丁? A: 在Linux应用中,作为DBA,我们知道MySQL跑在Linux系统之上,数据库最重要的追求就是性能,“稳”是重中之重,所以不能动不动就是换系统或是换这换那 ...

  9. git 的补丁使用方法

    1.生成补丁 format-patch可以基于分支进行打包,也可以基于上几次更新内容打包. 基于上几次内容打包 git format-patch HEAD^  有几个^就会打几个patch,从最近一次 ...

随机推荐

  1. mybaits源码分析(一)

    一.源码下载 1.手动编译源码 为了方便在看源码的过程中能够方便的添加注释,可以从官网下载源码编译生成对应的Jar包,然后上传到本地maven仓库,再引用这个Jar. 首先需要编译打包parent项目 ...

  2. vue element-ui 做分页功能之封装

    在 vue 项目中的 components 中 创建一个 文件夹,文件夹里创建一个 name(这个名字你随意取).vue <template>   <div class=" ...

  3. Django+Ansible构建任务中心思路

    Ansible作为老牌的自动化运维工具,由Python开发,应用广泛,但其默认只提供了命令行下的使用方式,好在提供有完善的API支持二次开发,可以很方便的集成到我们的自动化运维系统中 最近一个朋友跳槽 ...

  4. GoLang设计模式04 - 单例模式

    单例模式恐怕是最为人熟知的一种设计模式了.它同样也是创建型模式的一种.当某个struct只允许有一个实例的时候,我们会用到这种设计模式.这个struct的唯一的实例被称为单例对象.下面是需要创建单例对 ...

  5. js判断是在移动端还是在pc端

    function chatQQ3(){ if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) { //移动端打开 ...

  6. RSA及其证明 [原创]

    描述RSA的实现步骤介绍文章非常多,但说明并证明其原理,并进而讨论为什么这样设计的文章不多.本人才疏学浅,不敢说理解了R.S.A.三位泰斗的设计初衷,简单就自己的理解写一写,博大家一笑. 以下原创内容 ...

  7. io流-缓冲流

    单独去数据时,数据按块读入缓冲区,其后的操作则直接访问缓冲区 但是用 BufferedInputStream读取字节文件时,

  8. angularjs $http.get 和 $http.post 传递参数

    $http.get请求数据的格式 $http.get(URL,{ params: { "id":id } }) .success(function(response, status ...

  9. 【OI】蛇形填数题的深入探究

    题目:在 n×n 方阵里填入 1,2,...n×n, 要求蛇形填数.例如,n=4 时方阵为: 10  11  12  1 9    16  13  2 8    15  14  3 7     6  ...

  10. 在PHP中使用SPL库中的对象方法进行XML与数组的转换

    虽说现在很多的服务提供商都会提供 JSON 接口供我们使用,但是,还是有不少的服务依然必须使用 XML 作为接口格式,这就需要我们来对 XML 格式的数据进行解析转换.而 PHP 中并没有像 json ...