• 【Git】git revert 命令(撤销 commit 改动)


    基本语法

    Git revert命令用于通过创建一个新的commit来撤销一个或多个之前的commit。这样做的好处是保持了项目历史的完整性,并且可以将撤销的改动应用到其他分支。

    1. 撤销单个commit:如果你只想撤销一个commit,你可以使用以下命令
    git revert <commit_hash>
    
    git revert 131b7a916560c549e598ca9c66c2a3d28c7508e9
    
    • 1
    • 2
    • 3
    1. 撤销多个commit:如果你想撤销多个commit,你可以使用以下命令
    git revert <commit_hash1>..<commit_hash2>
    
    git revert 131b7a916560c549e598ca9c66c2a3d28c7508e9..7a5b4709656e614deab37eb19e355ba9e724eb
    
    • 1
    • 2
    • 3
    1. 使用交互式revert:如果你想在撤销多个commits时选择性地进行,你可以使用-i选项进入交互式模式
    git revert -i
    
    • 1

    使用案例

    1. 假设本次提交commit页面有问题,需要回退
    # 查看最近 2 次commit记录git log -2
    commit 1111fec75f226f83822f6ceda92704d39f3d443
    Author: 流星
    Date:   Mon Apr 22 14:24:11 2024 +0800
    
        fix: 本次发布的commit代码改动"
    
    commit 22229139486c6a59399b581df1c060b5f5846cf0
    Author: 流星
    Date:   Mon Apr 22 11:18:01 2024 +0800
    
        feat: 历史页面
    ~
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    1. 选择需要撤销的 commit 版本号,输入:wq并回车确定
    git revert 1111fec75f226f83822f6ceda92704d39f3d443
    Revert "fix: 本次发布的commit代码改动""
    
    This reverts commit 1111fec75f226f83822f6ceda92704d39f3d443.
    
    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    #
    # On branch lsmas
    # Your branch is up to date with 'origin/test'.
    #
    # Changes to be committed:
    #       modified:   src/pages/ListDetailPage/index.tsx
    #
    ~                                                                                                                                                                                                                                                
    ~                                                                                                                                                                                                                                                
    ~                                                                                                                                                                                                                                                
    ~                                                                                                                                                                                                                                                
    ~                                                                                                                                                                                                                                                
    ~                                                                                                                                                                                                                                                
    ~                                                                                                                                                                                                                                                
    ~                                                                                                                                                                                                                                                
    ~                                                                                                                                                                                                                                                
    ~                                                                                                                                                                                                                                                
    ~                                                                                                                                                                                                                                                
    ~                                                                                                                                                                                                                                                
    ~                                                                                                                                                                                                                                                
    :wq
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    1. 这就是成功的提示信息,这时候这条commit上的所有文件改动都会复原。
    git revert aa69cfec75f226f83822f6ceda92704d39f3d443
    [lsmas 74239f9] Revert "fix: 本次发布的commit代码改动""
     1 file changed, 26 insertions(+), 5 deletions(-)
    
    • 1
    • 2
    • 3
    1. 再次查看commit记录就会发现,多了一条回退的记录。
    git log -3
    commit 33339f9dcc7f4c094ae3dd07243048772338bdc3
    Author: 流星
    Date:   Mon Apr 22 14:52:31 2024 +0800
    
        Revert "fix: 本次发布的commit代码改动"
        
        This reverts commit 1111fec75f226f83822f6ceda92704d39f3d443.
    
    commit 1111fec75f226f83822f6ceda92704d39f3d443
    Author: 流星
    Date:   Mon Apr 22 14:24:11 2024 +0800
    
        fix: 本次发布的commit代码改动"
    
    commit 22229139486c6a59399b581df1c060b5f5846cf0
    Author: 流星
    Date:   Mon Apr 22 11:18:01 2024 +0800
    
        feat: 历史页面
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    1. 结束后再重新 push 发布,用回退后的版本代码覆盖有问题的代码。
  • 相关阅读:
    关于useEvent的思考
    如何克服发票自动化的 4 个最常见障碍
    【2022年】Python自动化测试的趋势,你还在每天很焦虑?
    14.8 Socket 一收一发通信
    ssm项目整合,简单的用户管理系统
    Windows cmd命令
    全民拼购是什么?剖析解读其优势和玩法
    22-07-20 西安 SpringCloud(01)
    C/C++内存管理学习【new】
    Selenium模拟浏览器访问
  • 原文地址:https://blog.csdn.net/qq_45677671/article/details/138076626