做网站前端开发的必备软件,wordpress 分类 文章数量,提升学历官网报名多少钱,提供秦皇岛网站建设哪家好目录
撤销最新合并的一次提交
撤销最新合并的多次提交
撤销中间合并某一个提交
注意 在Git中#xff0c;合并分支是一个常见的操作#xff0c;但有时候可能会意外地将错误的提交合并到了主分支。这时候需要撤销已合并的提交并恢复到正确的状态。本文将介绍的是如何在Git中…目录
撤销最新合并的一次提交
撤销最新合并的多次提交
撤销中间合并某一个提交
注意 在Git中合并分支是一个常见的操作但有时候可能会意外地将错误的提交合并到了主分支。这时候需要撤销已合并的提交并恢复到正确的状态。本文将介绍的是如何在Git中撤销已合并的提交无论这个提交记录是最新的还是中间的某一个。
撤销最新合并的一次提交 如果要撤销最新的合并提交可以使用git revert命令来创建一个新的提交撤销错误的变更。 首先使用git log命令查看提交历史找到最新的合并提交。
$ git log
commit c3d2e9a4e2a1e285ff4d8f06e01d4e3f19b532ea (HEAD - master)
Author: Hanmeimei hanmeimeiexample.com
Date: Fri Jun 30 15:26:43 2023 0800Incorrect merge commitcommit 3e5fb8a7b631eb6492ef32e28a813084d4d3de2b
Author: Lilei Lileiexample.com
Date: Thu Jun 29 18:20:56 2023 0800Correct commit...在上面的示例中大家可以看到最新的错误合并提交Incorrect merge commit。
使用git revert命令撤销合并提交并创建一个新的提交来还原到正确的状态。
$ git revert c3d2e9a4e2a1e285ff4d8f06e01d4e3f19b532eaGit将自动创建一个新的提交撤销错误的合并提交。
使用git log或git show命令验证新的提交历史确认错误的变更已经被撤销。
$ git log
commit b254d0f063b4ab4e7b78fb42015e0c55e0e98712 (HEAD - master)
Author: Hanmeimei hanmeimeiexample.com
Date: Fri Jun 30 15:46:28 2023 0800Revert Incorrect merge commitThis reverts commit c3d2e9a4e2a1e285ff4d8f06e01d4e3f19b532ea.commit 3e5fb8a7b631eb6492ef32e28a813084d4d3de2b
Author: Lilei lileiexample.com
Date: Thu Jun 29 18:20:56 2023 0800Correct commit...在这里可以看到一个新的提交Revert Incorrect merge commit被创建它撤销了最新的错误合并提交。
撤销最新合并的多次提交
如果要撤销最新合并的多次提交可以使用git reset命令来回滚到某次提交。以下是步骤
首先使用git log命令查看提交历史找到要回滚的哈希值。
$ git log
commit c5b890eee2edf9a353ec6bba0543e41d2529a8f8 (HEAD - master)
Author: Hanmeimei hanmeimeiexample.com
Date: Mon Jul 3 15:12:10 2023 0800Incorrect merge commitcommit 82bcf43083a4dc8c87091ebde4dd5374f0c6e274
Author: Hanmeimei hanmeimeiexample.com
Date: Mon Jul 3 15:11:54 2023 0800Incorrect merge commit2commit 60a52b00d0ee2703156231e209e8aad115919aee
Author: Hanmeimei hanmeimeiexample.com
Date: Mon Jun 26 06:32:35 2023 0000Correct commit...在这里需要回滚到Correct commit。
使用git reset 命令撤销合并提交并创建一个新的提交来还原到正确的状态。
$ git reset --soft 60a52b00d0ee2703156231e209e8aad115919aee
// 暂存区$ git reset --hard 60a52b00d0ee2703156231e209e8aad115919aee
// HEAD 指向此次提交记录$ git push origin HEAD --force
// 强制推送远端注意此次提交之后的修改不做任何保留git status查看工作区是没有记录的。
最后使用git log或git show命令验证提交历史确认错误的变更已经被撤销。
$ git log
commit 60a52b00d0ee2703156231e209e8aad115919aee (HEAD - master)
Author: Hanmeimei hanmeimeiexample.com
Date: Mon Jun 26 06:32:35 2023 0000Correct commit...如果出现了误删可以用以下办法来恢复
$ git reset --hard 82bcf43083a4dc8c87091ebde4dd5374f0c6e274
// 误删的哈希值HEAD is now at 82bcf4308 feat: Incorrect merge commit2$ git push origin HEAD --force
// 强制推送远端
撤销中间合并某一个提交
如果要撤销中间的某一个合并提交可以使用git revert命令并指定要撤销的提交哈希值。
首先使用git log命令查看提交历史并找到要撤销的中间合并提交。
$ git log
commit 3e5fb8a7b631eb6492ef32e28a813084d4d3de2b (HEAD - master)
Author: Lilei lileiexample.com
Date: Wed Jun 21 12:00:00 2023 0000Correct commitcommit a1b2c3d4e5f6a7b8c9d0e1f2g3h4i5j6k7l8m9n0
Author: Hanmeimei hanmeimeiexample.com
Date: Fri Jun 18 12:00:00 2023 0000Incorrect merge commit...在这里可以看到一个中间的错误合并提交Incorrect merge commit。
然后使用git revert命令撤销合并提交并创建一个新的提交来还原到正确的状态。
$ git revert a1b2c3d4e5f6a7b8c9d0e1f2g3h4i5j6k7l8m9n0Git将自动创建一个新的提交撤销错误的合并提交。
最后使用git log或git show命令验证新的提交历史确认错误的变更已经被撤销。
$ git log
commit b254d0f063b4ab4e7b78fb42015e0c55e0e98712 (HEAD - master)
Author: Hanmeimei hanmeimeiexample.com
Date: Mon Jun 28 12:10:00 2023 0000Revert Incorrect merge commitThis reverts commit a1b2c3d4e5f6a7b8c9d0e1f2g3h4i5j6k7l8m9n0.commit 3e5fb8a7b631eb6492ef32e28a813084d4d3de2b
Author: Lilei lileiexample.com
Date: Wed Jun 21 12:00:00 2023 0000Correct commitcommit a1b2c3d4e5f6a7b8c9d0e1f2g3h4i5j6k7l8m9n0
Author: Hanmeimei hanmeimeiexample.com
Date: Fri Jun 18 12:00:00 2023 0000Incorrect merge commit...可以看到之前的提交仍会保留在git log中而此次撤销会做为一次新的提交Revert Incorrect merge commit。
注意 在执行撤销操作之前请一定要确保你了解操作的后果并在进行任何更改之前创建备份或保存重要的数据。