简体   繁体   English

如何使用分离的 HEAD 提交覆盖提交?

[英]How to overwrite a commit with detached HEAD commit?

I have a branch that looks like this我有一个看起来像这样的分支

---A-----------B
    \
     \
      C (Detached HEAD)

I got it by this process:我通过这个过程得到了它:

git checkout branch
git checkout HEAD~
git commit -m "C"

and I want C to overwrite B (discard all changes in B)我希望C覆盖B (丢弃B所有更改)

Best would be without merging temp branch because I want the branch graph to look as simple as possible最好不要合并临时分支,因为我希望分支图看起来尽可能简单

Force delete the branch and create branch on the C commit.强制删除branch并在C提交上创建分支。 Do force push if branch is in any remote repository.如果分支在任何远程存储库中,请强制推送。

If you can repeate the commit (ie you have a diff saved somewhere), you can reset branch's HEAD to HEAD~ and then do the commit.如果您可以重复提交(即您在某处保存了差异),您可以将分支的HEAD重置为HEAD~然后进行提交。

git checkout <C>

git branch -D branch

git checkout -b branch

Your graph will become:您的图表将变为:

---A-----C <== branch
    \
     \---B

If B is already in the remote repository, use "git push -f" to force the push.如果 B 已经在远程仓库中,使用“git push -f”强制推送。 B will be garbage collected in the future. B 将来会被垃圾回收。

git checkout sha1ofcommittokeep git push --force-with-lease origin HEAD:branchtooverwrite

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM