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

基本语法

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

  1. 撤销单个commit:如果你只想撤销一个commit,你可以使用以下命令
git revert <commit_hash>

git revert 131b7a916560c549e598ca9c66c2a3d28c7508e9
  1. 撤销多个commit:如果你想撤销多个commit,你可以使用以下命令
git revert <commit_hash1>..<commit_hash2>

git revert 131b7a916560c549e598ca9c66c2a3d28c7508e9..7a5b4709656e614deab37eb19e355ba9e724eb
  1. 使用交互式revert:如果你想在撤销多个commits时选择性地进行,你可以使用-i选项进入交互式模式
git revert -i

使用案例

  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. 选择需要撤销的 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. 这就是成功的提示信息,这时候这条commit上的所有文件改动都会复原。
git revert aa69cfec75f226f83822f6ceda92704d39f3d443
[lsmas 74239f9] Revert "fix: 本次发布的commit代码改动""
 1 file changed, 26 insertions(+), 5 deletions(-)
  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. 结束后再重新 push 发布,用回退后的版本代码覆盖有问题的代码。

相关推荐

  1. 【Git】git revert 命令撤销 commit 改动

    2024-04-23 01:14:01       22 阅读
  2. git撤销命令大全

    2024-04-23 01:14:01       31 阅读
  3. Git撤回指定commit不保留更改

    2024-04-23 01:14:01       15 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-23 01:14:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-23 01:14:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-23 01:14:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-23 01:14:01       18 阅读

热门阅读

  1. 深度学习概念

    2024-04-23 01:14:01       12 阅读
  2. AJAX请求(axios篇)

    2024-04-23 01:14:01       14 阅读
  3. UDF小白入门

    2024-04-23 01:14:01       14 阅读
  4. git工具的安装及使用

    2024-04-23 01:14:01       12 阅读
  5. backtracking Leetcode 回溯算法题

    2024-04-23 01:14:01       11 阅读
  6. Linux文本处理三剑客:awk、grep和sed

    2024-04-23 01:14:01       13 阅读
  7. js高级 笔记03

    2024-04-23 01:14:01       11 阅读
  8. FastJson的使用

    2024-04-23 01:14:01       13 阅读