我在 github 上做了错误的提交,我想做以下事情:
- 从与 master 不同的分支 check out 旧提交
- 将此提交作为我的最后一次提交(删除此后的提交)
- 将此分支设为主分支并删除所有其他分支
- 将主分支合并到新分支
可以这样做吗?
最佳答案
关键命令是:
git checkout -B master aSHA1
如果您知道要在何处重置主分支,那么只需一行即可完成此操作。
-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/