如何处理在分离头中进行的提交
使用 git 我做了这样的事情
git clone
git checkout {a rev number tree rev before} (here I started to be in a detached head state)
//hacking
git commit
//hacking
git commit
(some commit where made on origin/master)
git pull (which does complete because there was some error due to the fact that I'm no more on master)
因为它告诉我,当我处于分离的头部状态时我仍然可以提交,我就这样做了。 但现在我想合并我的独立头分支和本地主分支,然后将我的一堆更改推送到 origin/master。
所以我的问题是如何将主分支与我的实际状态(分离头)合并
Using git I made something like this
git clone
git checkout {a rev number tree rev before} (here I started to be in a detached head state)
//hacking
git commit
//hacking
git commit
(some commit where made on origin/master)
git pull (which does complete because there was some error due to the fact that I'm no more on master)
Because it said to me that I can still commit when in a detached head state, I did so.
But now I want to like merge my detached head branch and my local master branch, and then push my bunch of changes to origin/master.
So my question is how could I merge the master branch with my actual state (detached head)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
在您所在的位置创建一个分支,然后切换到 master 并合并它:
Create a branch where you are, then switch to master and merge it:
你可以做这样的事情。
更简单的是
,但这有一点危险,如果你犯了一个错误,恢复在分离头上所做的提交可能会有点困难。
You could do something like this.
Even simpler would be
but this has the slight danger that if you do make a mistake it can be a little harder to recover the commits made on the detached head.
这就是我所做的:
基本上,将
分离的 HEAD
视为一个没有名称的新分支。您可以像任何其他分支一样提交到此分支。完成提交后,您想将其推送到远程。因此,您需要做的第一件事就是为这个
分离的 HEAD
命名。您可以轻松地在这个分离的 HEAD
上做到这一点:现在您可以像任何其他分支一样将其推送到远程。
就我而言,我还想将此分支与我在分离的 HEAD 中所做的提交(现在是一些新分支)一起快速转发到 master。我所做的就是:
当然,我后来将它合并到了
master
。就是这样。
This is what I did:
Basically, think of the
detached HEAD
as a new branch, without name. You can commit into this branch just like any other branch. Once you are done committing, you want to push it to the remote.So the first thing you need to do is give this
detached HEAD
a name. You can easily do it like, while being on thisdetached HEAD
:Now you can push it to remote like any other branch.
In my case, I also wanted to fast-forward this branch to master along with the commits I made in the
detached HEAD
(nowsome-new-branch
). All I did was:Of course, I merged it later to
master
.That's about it.
您可以只执行
git merge
或gitcherry-pick; <提交> ...
正如 Ryan Stewart 所建议的,您还可以从当前的 HEAD 创建一个分支:
或者只是一个标签:
You can just do
git merge <commit-number>
orgit cherry-pick <commit> <commit> ...
As suggested by Ryan Stewart you may also create a branch from the current HEAD:
Or just a tag:
或者,您可以将 commit-id 挑选到您的分支上。
git checkout master
gitcherry-pick
没有临时分支,没有合并。
Alternatively, you could cherry-pick the commit-id onto your branch.
<commit-id> made in detached head state
git checkout master
git cherry-pick <commit-id>
No temporary branches, no merging.
我看到每个人几乎每个人都提出了创建临时分支的解决方案。现在,我们需要承认,每当出现这种“在分离状态下提交”问题时,通常会在一次提交后检测到。并为那个微不足道的提交创建一个完整的分支 - 听起来太多了,对吧?尤其是在您已经在太多分支之间跳转的项目中。
那有什么简单的方法呢? 使用提交哈希!
我如何获得它?
git log
。您会看到类似这样的内容:现在,虽然它看起来像正常情况,但是当您执行 git push 时,它会显示“Everything up-to-date”。
细心的人会发现它不是“最新的”。
HEAD
位于 master 之外的其他位置。10bf8fe4d1
的初始字符即可。首先,执行git checkout master
。这会将您移至master
分支。现在你已经复制了哈希值。您可以执行git merge
。现在执行 git log瞧:
现在,HEAD 似乎位于正确的位置。
有人可能会问,“如果我没有哈希怎么办?我对悬空提交一无所知,只是做了一个
git checkout master
。”别担心,我已经为你做好了准备。您可以在两个地方找到提交哈希:git checkout master
时,git
已经这样警告您您可以看到您的宝藏(
hash),对吧?
git reflog
。它会向您显示类似这样的内容:您看到那里有您正在寻找的宝藏......哈希。
我想这涵盖了在带有悬空提交的分离状态下可能发生的所有可能情况。下次小心!!
I see everyone almost everyone has suggested a solution where a temporary branch is being created. Now, one needs to admit that whenever this "committed in a detached state" issue arises, it's generally detected after one commit. And creating one whole branch for that one puny commit - Sounds too much, right? Especially in projects where you are already jumping around among too many branches.
What's the easy way then? Use the commit hash!
How do I get that?
git log
. You would see something like this:Now, although it looks like a normal case, but when you do a
git push
it would say "Everything up-to-date".A careful person would see that it's not "up-to-date".
HEAD
is somewhere other than the master.10bf8fe4d1
. And first, do agit checkout master
. This would move you tomaster
branch. And now since you have already copied the hash. You can do agit merge <hash>
. Do agit log
nowAnd VOILA:
Now, the
HEAD
seems to be in a proper place.Someone might ask, "What if I don't have the hash? I didn't know anything about the dangling commit and just did a
git checkout master
." Don't worry, I've got you covered. You can find the commit hash in two places:git checkout master
,git
had warned you like thisYou can see your treasure (
hash
), right?git reflog
. It will show you something like this:You see that there's the treasure you were looking for... The hash.
I guess this covers all possible scenarios that could happen in a detached state with a dangling commit. Beware next time!!
在分离 HEAD 的情况下,提交会像平常一样工作,除了没有命名分支被更新。要使用您提交的更改更新 master 分支,请在您所在的位置创建一个临时分支(这样临时分支将包含您在分离的 HEAD 中所做的所有提交的更改),然后切换到 master 分支并将临时分支与主人。
In case of detached HEAD, commits work like normal, except no named branch gets updated. To get master branch updated with your committed changes, make a temporary branch where you are (this way the temporary branch will have all the committed changes you have made in the detached HEAD) , then switch to the master branch and merge the temporary branch with the master.
如果您处于这种情况(提交被分离),我将给出适当的步骤,直到推送。分离的意思是当您的提交不与任何分支关联时,它仅与“分离头”状态关联。
首先你必须创建一个新分支,分离的提交将自动与新分支关联
并转到 master 结账,以便你可以合并该“临时工作”。请考虑检查一下分支名称列表,以避免出现一些错误
,并且git会说你的文件可能有冲突,回去查看你的代码文本编辑器,并删除你不需要的代码。
提交该更改
,然后删除“temporaryWork”
并将其推送到 github 中。
i will give a proper step until push if you in that condition (the commit being detached). the detached mean is when your commit is not is not associated with any branches, it is only associated with the "detached head" state.
First u must to make a new branch, the detached commit will be automatically associated with the new branch
And go to checkout to master so u can merge that "temporaryWork". Please consider to check it the list of branch name, to avoiding some error
And git will said your file may be conflicted, go to look back your code text editor, and delete the code that you not need.
And commit that change
then delete the "temporaryWork"
and push it into github.
一个简单的解决方法是为该提交创建一个新分支并签出它: git checkout -b; <提交哈希>。
这样,您所做的所有更改都将保存在该分支中。如果您需要清理 master 分支中剩余的提交,请务必运行 git reset --hard master 。
这样,您将重写您的分支,因此请确保不要因这些更改而打扰任何人。请务必阅读本文,以更好地说明 分离 HEAD 状态。
An easy fix is to just create a new branch for that commit and checkout to it:
git checkout -b <branch-name> <commit-hash>
.In this way, all the changes you made will be saved in that branch. In case you need to clean up your master branch from leftover commits be sure to run
git reset --hard master
.With this, you will be rewriting your branches so be sure not to disturb anyone with these changes. Be sure to take a look at this article for a better illustration of detached HEAD state.
签出实际分支
git merge {{commit-hash}}
checkout actual-branch
git merge {{commit-hash}}
当我检查导致头部分离时,git 实际上告诉我在这种情况下该怎么做:
如果执行此操作,它会将 HEAD 分支保留为分离头之前的状态。它会创建一个新分支,其中包含在分离头状态下工作时所做的所有提交,就像您从 HEAD 位置执行了 git checkout一样。之后,如您所料,HEAD 将成为新分支的负责人。
要更详细地重现/测试/理解:
在德语中,git 的响应是:
用英语,git 的响应是:
When I did the checkout leading to detached head, git actually tells me what to do in such a case:
If you do this, it leaves the HEAD branch as it was before detaching the head. It creates a new branch with all the commits made while working in the detached head state, as if you had done
git checkout <new-branch-name>
from the HEAD location. Afterwards, HEAD will be the head of the new branch, as you would expect.To reproduce/test/understand in more details:
In German, the response from git is:
In English, the response from git is:
也许不是最好的解决方案(将重写历史记录),但您也可以执行 git reset --hard <分离头提交的哈希>。
Maybe not the best solution, (will rewrite history) but you could also do
git reset --hard <hash of detached head commit>
.我更喜欢的最直接的方法是,因为我的意思是提交到当前分支并推送到远程而不需要中间临时分支,这是由来自 git 的消息建议的:
所以我的工作流程是(假设我是on
main
):现在我已将更改推送到远程子模块,并将本地状态返回到我开始的分支的 HEAD。提交历史记录保持线性。
显然,在这种情况下,我正在推送到 main,但它可能是您用来固定子模块状态的任何分支。
The most direct way that I prefer because I mean to commit into the current branch and push to remote without an intermediate temporary branch, is suggested by the message from
git
:So my workflow is (assuming I'm on
main
):Now I have pushed changes to the submodule remote, and returned the local state to the HEAD of the branch I started on. The commit history stays linear.
Obviously in this case I'm pushing to main but it could be any branch you are using to pin your submodule state.