github - git checkout 提交并删除较新的提交

标签 github git-commit git-checkout

我在 github 上做了错误的提交,我想做以下事情:

  1. 从与 master 不同的分支 check out 旧提交
  2. 将此提交作为我的最后一次提交(删除此后的提交)
  3. 将此分支设为主分支并删除所有其他分支
  4. 将主分支合并到新分支

可以这样做吗?

最佳答案

关键命令是:

git checkout -B master aSHA1

如果您知道要在何处重置主分支,那么只需一行即可完成此操作。

来自 git checkout man page :

-B <new_branch> 

Creates the branch <new_branch> and start it at <start_point>;
if it already exists, then reset it to <start_point>. This is equivalent to running "git branch" with "-f";

然后你可以强制推送它:

git push --force

这将重置origin/master (GitHub 端的主分支)到旧的 SHA1。

<小时/>

注意:

在 Git 2.40(2023 年第 1 季度)中,对“checkout -b/-B ”进行了澄清,并解释了“ git branch [-f] ( man ) 的相似之处,但在文档中有所不同。

参见commit fedb8ea (2023 年 1 月 19 日)作者:Junio C Hamano ( gitster ) .
(由 Junio C Hamano -- gitster --commit 06f2b5f 合并,2023 年 1 月 30 日)

checkout: document -b/-B to highlight the differences from "git branch"

The existing text read as if "git checkout -b/-B name"(man) were equivalent to "git branch [-f] name"(man), which clearly was not what we wanted to say.

git checkout现在包含在其 man page 中:

Creates the branch <new-branch>, start it at <start-point>; if it already exists, then reset it to <start-point>.
And then check the resulting branch out.
This is equivalent to running "git branch" with "-f" followed by "git checkout" of that branch;

关于github - git checkout 提交并删除较新的提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15120526/

相关文章:

本地存储库的 Git checkout 修订版

git checkout 上一条

Git 要求输入错误 URL 的密码

git - 你如何不在 git 中添加未跟踪的文件?

git - 如何在git中将文件 "Un-push"?

git拒绝推送非快进

git - 是否可以同时查看同一个项目的多个git分支?

node.js - 安装 Atom 编辑器时出错

git - 使当前提交成为 Git 存储库中唯一的(初始)提交?

r - 撤消 Rstudio 中太大而无法推送的 git commit