如何处理在分离头中进行的提交

发布于 2024-11-30 20:28:06 字数 465 浏览 4 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

小忆控 2024-12-07 20:28:06

在您所在的位置创建一个分支,然后切换到 master 并合并它:

git switch -c my-temporary-work
git switch master
git merge my-temporary-work

Create a branch where you are, then switch to master and merge it:

git switch -c my-temporary-work
git switch master
git merge my-temporary-work
°如果伤别离去 2024-12-07 20:28:06

你可以做这样的事情。

# Create temporary branch for your detached head
git branch tmp

# Go to master
git checkout master

# Merge in commits from previously detached head
git merge tmp

# Delete temporary branch
git branch -d tmp

更简单的是

git checkout master
git merge HEAD@{1}

,但这有一点危险,如果你犯了一个错误,恢复在分离头上所做的提交可能会有点困难。

You could do something like this.

# Create temporary branch for your detached head
git branch tmp

# Go to master
git checkout master

# Merge in commits from previously detached head
git merge tmp

# Delete temporary branch
git branch -d tmp

Even simpler would be

git checkout master
git merge HEAD@{1}

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.

蓝礼 2024-12-07 20:28:06

这就是我所做的:

基本上,将分离的 HEAD 视为一个没有名称的新分支。您可以像任何其他分支一样提交到此分支。完成提交后,您想将其推送到远程。

因此,您需要做的第一件事就是为这个分离的 HEAD 命名。您可以轻松地在这个分离的 HEAD 上做到这一点:

git checkout -b some-new-branch

现在您可以像任何其他分支一样将其推送到远程。

就我而言,我还想将此分支与我在分离的 HEAD 中所做的提交(现在是一些新分支)一起快速转发到 master。我所做的就是:

git checkout master

git pull # to make sure my local copy of master is up to date

git checkout some-new-branch

git merge master # this added the current state of master to my changes

当然,我后来将它合并到了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 this detached HEAD:

git checkout -b some-new-branch

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 (now some-new-branch). All I did was:

git checkout master

git pull # to make sure my local copy of master is up to date

git checkout some-new-branch

git merge master # this added the current state of master to my changes

Of course, I merged it later to master.

That's about it.

习ぎ惯性依靠 2024-12-07 20:28:06

您可以只执行 git mergegitcherry-pick; <提交> ...

正如 Ryan Stewart 所建议的,您还可以从当前的 HEAD 创建一个分支:

git branch brand-name

或者只是一个标签:

git tag tag-name

You can just do git merge <commit-number> or git cherry-pick <commit> <commit> ...

As suggested by Ryan Stewart you may also create a branch from the current HEAD:

git branch brand-name

Or just a tag:

git tag tag-name
玩世 2024-12-07 20:28:06

或者,您可以将 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.

不顾 2024-12-07 20:28:06

我看到每个人几乎每个人都提出了创建临时分支的解决方案。现在,我们需要承认,每当出现这种“在分离状态下提交”问题时,通常会在一次提交后检测到。并为那个微不足道的提交创建一个完整的分支 - 听起来太多了,对吧?尤其是在您已经在太多分支之间跳转的项目中。

那有什么简单的方法呢? 使用提交哈希!

我如何获得它?

  1. 执行git log。您会看到类似这样的内容:
commit 10bf8fe4d17bb7de59586a7abb6db321f0786bb3 (HEAD)
Author: Someone <[email protected]>
Date:   So/me/day SO:ME:TI:ME 

    A commit message that doesn't mean much

commit a3cd1cedf1962916cdc2945f2bd2b271ec8b919d (origin/master, master)
Author: Someone <[email protected]>
Date:   Some/other/day SOME:OTHER:TIME 

    Another commit message that doesn't mean much

commit 1bfabbe09c70419070fe29ff1ed276c0207bbe10
Author: Someone <[email protected]>
Date:   Thu Jul 8 08:38:12 2021 +0530

    Enough reading the example, focus on the answer!! 

现在,虽然它看起来像正常情况,但是当您执行 git push 时,它会显示“Everything up-to-date”。

细心的人会发现它不是“最新的”。 HEAD 位于 master 之外的其他位置。

  1. 那么,接下来怎么办?只需复制哈希 10bf8fe4d1 的初始字符即可。首先,执行 git checkout master。这会将您移至 master 分支。现在你已经复制了哈希值。您可以执行git merge。现在执行 git log

瞧:

commit 10bf8fe4d17bb7de59586a7abb6db321f0786bb3 (HEAD -> master)
Author: Someone <[email protected]>
Date:   S/om/eday SO:ME:TI:ME 

    A commit message that doesn't mean much

commit a3cd1cedf1962916cdc2945f2bd2b271ec8b919d (origin/master)
Author: Someone <[email protected]>
Date:   Some/other/day SOME:OTHER:TIME 

    Another commit message that doesn't mean much

commit 1bfabbe09c70419070fe29ff1ed276c0207bbe10
Author: Someone <[email protected]>
Date:   Thu Jul 8 08:38:12 2021 +0530

    Enough reading the example, focus on the answer!! 

现在,HEAD 似乎位于正确的位置。

有人可能会问,“如果我没有哈希怎么办?我对悬空提交一无所知,只是做了一个git checkout master。”别担心,我已经为你做好了准备。您可以在两个地方找到提交哈希:

  1. 当您执行git checkout master时,git已经这样警告您
Warning: you are leaving 1 commit behind, not connected to
any of your branches:

  10bf8fe A commit message that doesn't mean much

If you want to keep it by creating a new branch, this may be a good time
to do so with:

 git branch <new-branch-name> 10bf8fe

Switched to branch 'master'

您可以看到您的宝藏(hash),对吧?

  1. 别告诉我你找不到它。它就在那里。但如果你真的不能,那么你可以执行git reflog。它会向您显示类似这样的内容:
a3cd1ce (HEAD -> master, origin/master) HEAD@{0}: checkout: moving from 10bf8fe4d17bb7de59586a7abb6db321f0786bb3 to master
10bf8fe HEAD@{1}: commit:  A commit message that doesn't mean much

您看到那里有您正在寻找的宝藏......哈希。

我想这涵盖了在带有悬空提交的分离状态下可能发生的所有可能情况。下次小心!!

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?

  1. Do a git log. You would see something like this:
commit 10bf8fe4d17bb7de59586a7abb6db321f0786bb3 (HEAD)
Author: Someone <[email protected]>
Date:   So/me/day SO:ME:TI:ME 

    A commit message that doesn't mean much

commit a3cd1cedf1962916cdc2945f2bd2b271ec8b919d (origin/master, master)
Author: Someone <[email protected]>
Date:   Some/other/day SOME:OTHER:TIME 

    Another commit message that doesn't mean much

commit 1bfabbe09c70419070fe29ff1ed276c0207bbe10
Author: Someone <[email protected]>
Date:   Thu Jul 8 08:38:12 2021 +0530

    Enough reading the example, focus on the answer!! 

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.

  1. So, what next? Just copy the initial chars of the hash 10bf8fe4d1. And first, do a git checkout master. This would move you to master branch. And now since you have already copied the hash. You can do a git merge <hash>. Do a git log now

And VOILA:

commit 10bf8fe4d17bb7de59586a7abb6db321f0786bb3 (HEAD -> master)
Author: Someone <[email protected]>
Date:   S/om/eday SO:ME:TI:ME 

    A commit message that doesn't mean much

commit a3cd1cedf1962916cdc2945f2bd2b271ec8b919d (origin/master)
Author: Someone <[email protected]>
Date:   Some/other/day SOME:OTHER:TIME 

    Another commit message that doesn't mean much

commit 1bfabbe09c70419070fe29ff1ed276c0207bbe10
Author: Someone <[email protected]>
Date:   Thu Jul 8 08:38:12 2021 +0530

    Enough reading the example, focus on the answer!! 

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:

  1. When you did the git checkout master, git had warned you like this
Warning: you are leaving 1 commit behind, not connected to
any of your branches:

  10bf8fe A commit message that doesn't mean much

If you want to keep it by creating a new branch, this may be a good time
to do so with:

 git branch <new-branch-name> 10bf8fe

Switched to branch 'master'

You can see your treasure (hash), right?

  1. Don't tell me you can't find it. It's right there. But if you really can't, then you can do a git reflog. It will show you something like this:
a3cd1ce (HEAD -> master, origin/master) HEAD@{0}: checkout: moving from 10bf8fe4d17bb7de59586a7abb6db321f0786bb3 to master
10bf8fe HEAD@{1}: commit:  A commit message that doesn't mean much

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!!

花海 2024-12-07 20:28:06

在分离 HEAD 的情况下,提交会像平常一样工作,除了没有命名分支被更新。要使用您提交的更改更新 master 分支,请在您所在的位置创建一个临时分支(这样临时分支将包含您在分离的 HEAD 中所做的所有提交的更改),然后切换到 master 分支并将临时分支与主人。

git branch  temp
git checkout master
git merge temp

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.

git branch  temp
git checkout master
git merge temp
嘿咻 2024-12-07 20:28:06

如果您处于这种情况(提交被分离),我将给出适当的步骤,直到推送。分离的意思是当您的提交不与任何分支关联时,​​它仅与“分离头”状态关联。

首先你必须创建一个新分支,分离的提交将自动与新分支关联

git branch temporaryWork

并转到 master 结账,以便你可以合并该“临时工作”。请考虑检查一下分支名称列表,以避免出现一些错误

git checkout master
git merge temporaryWork

,并且git会说你的文件可能有冲突,回去查看你的代码文本编辑器,并删除你不需要的代码。

提交该更改

git add .
git commit -am "merging temporaryWork" 

,然后删除“temporaryWork”

get branch -d temporaryWork

并将其推送到 github 中。

git push origin master

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

git branch temporaryWork

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

git checkout master
git merge temporaryWork

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

git add .
git commit -am "merging temporaryWork" 

then delete the "temporaryWork"

get branch -d temporaryWork

and push it into github.

git push origin master
怀里藏娇 2024-12-07 20:28:06

一个简单的解决方法是为该提交创建一个新分支并签出它: 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.

不寐倦长更 2024-12-07 20:28:06

签出实际分支

git merge {{commit-hash}}

checkout actual-branch

git merge {{commit-hash}}

倾城泪 2024-12-07 20:28:06

当我检查导致头部分离时,git 实际上告诉我在这种情况下该怎么做:

git switch -c

如果执行此操作,它会将 HEAD 分支保留为分离头之前的状态。它会创建一个新分支,其中包含在分离头状态下工作时所做的所有提交,就像您从 HEAD 位置执行了 git checkout一样。之后,如您所料,HEAD 将成为新分支的负责人。

要更详细地重现/测试/理解:

  1. 创建一个包含两次提交的 testrepo:
~/gittest$ git log --oneline 
17c34c0 (HEAD -> 主控) 2
5975930 1
  1. 签出之前的提交 1

~/gittest$ git checkout 5975930

在德语中,git 的响应是:

注释:Wechsle zu '5975930'。

Sie befinden sich im Zustand eines 'losgelösten HEAD'。 Sie können sich
umschauen、实验、实验和承诺,
und Sie können alle möglichen Commits, die Sie in diesem Zustand
machen, ohne Auswirkungen auf irgendeinen Branch verwerfen, indem Sie
zu einem anderen Branch wechseln。

Wenn Sie einen neuen Branch erstellen möchten, um Ihre erstellten
Commits zu behalten, können Sie das (jetzt oder später) durch Nutzung
von 'switch' mit der 选项 -c tun.贝斯皮尔:

git switch -c \

Oder um diese Operation rückgängig zu machen:git switch -

Sie können diesen Hinweis ausschalten, indem Sie die
配置变量“advice.detachedHead”auf“false”setzen。

HEAD ist jetzt bei 5975930 1

用英语,git 的响应是:

注意:切换到“5975930”。

您处于“分离 HEAD”状态。你可以环顾四周,进行实验
更改并提交它们,并且您可以放弃在此所做的任何提交
状态不会通过切换到分支而影响任何分支。

如果您想创建一个新分支来保留您创建的提交,您可以
通过将 -c 与 switch 命令一起使用来执行此操作(现在或稍后)。示例:

git switch -c.

或者撤消此操作:

git 开关 -

通过将配置变量advice.detachedHead设置为 false 来关闭此建议

HEAD 现在位于 5975930 1

When I did the checkout leading to detached head, git actually tells me what to do in such a case:

git switch -c <new-branch-name>

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:

  1. creating a testrepo with two commits:
~/gittest$ git log --oneline 
17c34c0 (HEAD -> master) 2
5975930 1
  1. checkout previous commit 1

~/gittest$ git checkout 5975930

In German, the response from git is:

Hinweis: Wechsle zu '5975930'.

Sie befinden sich im Zustand eines 'losgelösten HEAD'. Sie können sich
umschauen, experimentelle Änderungen vornehmen und diese committen,
und Sie können alle möglichen Commits, die Sie in diesem Zustand
machen, ohne Auswirkungen auf irgendeinen Branch verwerfen, indem Sie
zu einem anderen Branch wechseln.

Wenn Sie einen neuen Branch erstellen möchten, um Ihre erstellten
Commits zu behalten, können Sie das (jetzt oder später) durch Nutzung
von 'switch' mit der Option -c tun. Beispiel:

git switch -c \<neuer-Branchname>

Oder um diese Operation rückgängig zu machen: git switch -

Sie können diesen Hinweis ausschalten, indem Sie die
Konfigurationsvariable 'advice.detachedHead' auf 'false' setzen.

HEAD ist jetzt bei 5975930 1

In English, the response from git is:

Note: switching to '5975930'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

git switch -c <new-branch-name>.

Or to undo this operation with:

git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 5975930 1

爱她像谁 2024-12-07 20:28:06

也许不是最好的解决方案(将重写历史记录),但您也可以执行 git reset --hard <分离头提交的哈希>。

Maybe not the best solution, (will rewrite history) but you could also do git reset --hard <hash of detached head commit>.

掐死时间 2024-12-07 20:28:06

我更喜欢的最直接的方法是,因为我的意思是提交到当前分支并推送到远程而不需要中间临时分支,这是由来自 git 的消息建议的:

git push
# fatal: You are not currently on a branch.
# To push the history leading to the current (detached HEAD)
# state now, use

    git push origin HEAD:<name-of-remote-branch>

所以我的工作流程是(假设我是on main):

# make changes
git commit -am "these fixes"
git push origin HEAD:main
git checkout main
git pull

# In my case I'm usually making changes in a submodule, ignore this otherwise
# get back to parent repo
cd ..
git add path/to/submodule
git commit -m "updated submodule"
git push

# all done

现在我已将更改推送到远程子模块,并将本地状态返回到我开始的分支的 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:

git push
# fatal: You are not currently on a branch.
# To push the history leading to the current (detached HEAD)
# state now, use

    git push origin HEAD:<name-of-remote-branch>

So my workflow is (assuming I'm on main):

# make changes
git commit -am "these fixes"
git push origin HEAD:main
git checkout main
git pull

# In my case I'm usually making changes in a submodule, ignore this otherwise
# get back to parent repo
cd ..
git add path/to/submodule
git commit -m "updated submodule"
git push

# all done

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文