简体   繁体   English

如何从分支 git-commit 到分离的 HEAD?

[英]How to git-commit from a branch to a detached HEAD?

My ref HEAD points the branch foo , that points to the commit 123abc ;我的ref HEAD指向分支foo ,它指向提交123abc and I have some staged work.我有一些分阶段的工作。

How to git commit that work, moving the HEAD to the newly created commit, but without advancing the branch ?如何git commit该工作,将HEAD移动到新创建的提交,但不推进分支

(hence: leaving foo point to 123abc ) (因此:将foo指向123abc

Is it possible to do it with a single git command?是否可以使用单个git命令来完成?

Simply detach, then commit:只需分离,然后提交:

git checkout --detach
git commit -m "Commit as usual"

If HEAD is directly pointing to commit 123abc , it's already on detached HEAD state instead of on foo .如果HEAD直接指向提交123abc ,它已经在分离的 HEAD state 而不是foo上。 git commit will create a new commit and move HEAD to the new commit, leaving foo unmoved. git commit将创建一个新提交并将HEAD移动到新提交,而foo保持不动。

If HEAD points at refs/heads/foo and refs/heads/foo points at 123abc , you can run git checkout 123abc and then make the commit.如果HEAD指向refs/heads/foo并且refs/heads/foo指向123abc ,您可以运行git checkout 123abc然后进行提交。

You could just commit and then do a reset:您可以只提交然后进行重置:

# from foo branch
git commit -m 'your work here'
git reset --soft HEAD~1

This would move the HEAD pointer back one commit to 123abc , but would also stage all the work in that commit.这会将 HEAD 指针移回一个提交到123abc ,但也会暂存该提交中的所有工作。

There are other types of reset (mixed, hard), which do variants of the above.还有其他类型的重置(混合,硬),它们是上述的变体。 Without knowing your end goal, it isn't clear what kind of reset you should use.在不知道您的最终目标的情况下,不清楚您应该使用哪种重置方式。 In general, though, moving the HEAD back one commit is something you would do if you wanted to rewrite the prior HEAD commit.不过,一般来说,如果您想重写之前的 HEAD 提交,则将 HEAD 移回一个提交是您会做的事情。

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

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