【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
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一颗不甘坠落的流星

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

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

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

打赏作者

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

抵扣说明:

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

余额充值