梦见你的名字 发表于 2024-12-12 07:41:56

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

基本语法

   Git revert命令用于通过创建一个新的commit来撤销一个或多个之前的commit。如许做的利益是保持了项目汗青的完备性,并且可以将撤销的改动应用到其他分支。

[*]撤销单个commit:假如你只想撤销一个commit,你可以使用以下命令
git revert <commit_hash>

git revert 131b7a916560c549e598ca9c66c2a3d28c7508e9

[*]撤销多个commit:假如你想撤销多个commit,你可以使用以下命令
git revert <commit_hash1>..<commit_hash2>

git revert 131b7a916560c549e598ca9c66c2a3d28c7508e9..7a5b4709656e614deab37eb19e355ba9e724eb

[*]使用交互式revert:假如你想在撤销多个commits时选择性地进行,你可以使用-i选项进入交互式模式
git revert -i
使用案例


[*]假设本次提交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: 历史页面
~

[*]选择需要撤销的 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

[*]这就是成功的提示信息,这时候这条commit上的所有文件改动都会复原。
➜git revert aa69cfec75f226f83822f6ceda92704d39f3d443
Revert "fix: 本次发布的commit代码改动""
1 file changed, 26 insertions(+), 5 deletions(-)

[*]再次检察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: 历史页面


[*]竣事后再重新 push 发布,用回退后的版本代码覆盖有题目的代码。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 【Git】git revert 命令(撤销 commit 改动)