• GIT打补丁



    一. 准备工作:

    [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信息。

  • 相关阅读:
    International Collegiate Programming Contest 2019 Latin American Regional Contests E. Eggfruit Cake(思维/尺取)
    Codeforces Round #673 (Div. 2) C. k-Amazing Numbers(思维)
    2020 计蒜之道 预赛 第一场 A、B
    生成字符画
    我对目前国内教学的看法
    Stm32 调试时发生HardFault_Handler
    python异常处理
    windows nfs客户端配置
    linux服务器删除文件后df -h查看文件系统占比无变化
    python ssh小程序
  • 原文地址:https://www.cnblogs.com/wscsq789/p/15375360.html
Copyright © 2020-2023  润新知