Git revert命令用于通过创建一个新的commit来撤销一个或多个之前的commit。这样做的好处是保持了项目历史的完整性,并且可以将撤销的改动应用到其他分支。
git revert <commit_hash>
git revert 131b7a916560c549e598ca9c66c2a3d28c7508e9
git revert <commit_hash1>..<commit_hash2>
git revert 131b7a916560c549e598ca9c66c2a3d28c7508e9..7a5b4709656e614deab37eb19e355ba9e724eb
-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
[lsmas 74239f9] 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: 历史页面