当前位置: 代码网 > it编程>编程语言>其他编程 > 【Git】git revert 命令(撤销 commit 改动)

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

2024年08月01日 其他编程 我要评论
git revert 命令,用于撤销 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 发布,用回退后的版本代码覆盖有问题的代码。
(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com