使用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)

因为它告诉我,当我处于超然的头脑状态时,我仍然可以做出承诺,我这样做了。 但现在我想合并分离的head分支和本地master分支,然后将我的一堆更改推到origin/master。

所以我的问题是我如何将主分支与我的实际状态(分离头)合并


当前回答

如果你在这种情况下(提交被分离),我会给一个适当的步骤直到push。分离的意思是当你的提交不与任何分支相关联时,它只与“分离的头”状态相关联。

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

git branch temporaryWork

然后转到结帐master,这样你就可以合并那个“temporaryWork”。请考虑核对一下分公司名称列表,以免出现错误

git checkout master
git merge temporaryWork

git会提示你的文件可能有冲突,然后去查看你的代码文本编辑器,删除你不需要的代码。

并提交变更

git add .
git commit -am "merging temporaryWork" 

然后删除"temporaryWork"

get branch -d temporaryWork

然后把它推到github。

git push origin master

其他回答

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

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

我怎么得到它?

做一个git日志。你会看到这样的东西:

commit 10bf8fe4d17bb7de59586a7abb6db321f0786bb3 (HEAD)
Author: Someone <someone@something.com>
Date:   So/me/day SO:ME:TI:ME 

    A commit message that doesn't mean much

commit a3cd1cedf1962916cdc2945f2bd2b271ec8b919d (origin/master, master)
Author: Someone <someone@something.com>
Date:   Some/other/day SOME:OTHER:TIME 

    Another commit message that doesn't mean much

commit 1bfabbe09c70419070fe29ff1ed276c0207bbe10
Author: Someone <someone@something.com>
Date:   Thu Jul 8 08:38:12 2021 +0530

    Enough reading the example, focus on the answer!! 

现在,虽然它看起来像一个正常的情况,但当你执行git push时,它会说"Everything - updated "

细心的人会发现它不是“最新的”。HEAD不是master。

那么,接下来呢?只需复制散列10bf8fe4d1的初始字符。首先,执行git checkout master。这将把你移到主分支。现在既然你已经复制了哈希值。你可以做git merge <hash>。现在做git日志

瞧:

commit 10bf8fe4d17bb7de59586a7abb6db321f0786bb3 (HEAD -> master)
Author: Someone <someone@something.com>
Date:   S/om/eday SO:ME:TI:ME 

    A commit message that doesn't mean much

commit a3cd1cedf1962916cdc2945f2bd2b271ec8b919d (origin/master)
Author: Someone <someone@something.com>
Date:   Some/other/day SOME:OTHER:TIME 

    Another commit message that doesn't mean much

commit 1bfabbe09c70419070fe29ff1ed276c0207bbe10
Author: Someone <someone@something.com>
Date:   Thu Jul 8 08:38:12 2021 +0530

    Enough reading the example, focus on the answer!! 

现在,HEAD似乎在一个合适的地方。

有人可能会问,“如果我没有哈希怎么办?”我对悬垂提交一无所知,只是做了一个git checkout master。”别担心,我会帮你的。你可以在两个地方找到提交散列:

当你使用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'

你可以看到你的宝藏(哈希),对吗?

别告诉我你找不到。就在那儿。但如果你真的不能,你可以做一个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

你看,这就是你一直在寻找的宝藏……散列。

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

你可以使用git merge <commit-number>或者git cherry-pick <commit> <commit>…

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

git branch brand-name

或者只是一个标签:

git tag tag-name

这就是我所做的:

基本上,可以将分离的HEAD看作一个没有名称的新分支。你可以像其他分支一样提交到这个分支。一旦你完成了提交,你想把它推到遥控器。

因此,您需要做的第一件事是为这个分离的HEAD命名。你可以很容易地做到这一点,就像在这个分离的头部:

git checkout -b some-new-branch

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

在我的例子中,我还想快进这个分支,并在分离的HEAD(现在是某个新分支)中进行提交。我所做的就是:

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

当然,我后来把它合并了才掌握。

差不多就是这样。

结帐actual-branch

Git合并{{commit-hash}}

当我做了导致分离头部的签出,git实际上告诉我在这种情况下该怎么做:

Git开关-c \<new-branchname>

结果会像分离头之前一样离开主节点,而包含在分离头状态下工作时所做的所有提交的新分支。

重现/测试/了解更多细节:

用两次提交创建一个testrepo:

~/gittest$ git log——一行 17c34c0 (HEAD -> master 5975930 1

签出之前的提交1

~/gittest$ git checkout 5975930

这条德国信息出现了

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

翻译成英语是:

Note: Change to '5975930'. You are in the state of a 'detached HEAD'. You can look around, make experimental changes and commit them, and you can discard any possible commits you make in this state without affecting any branch by switching to another branch. If you want to create a new branch to keep your created commits, you can do that (now or later) by using 'switch' with the -c option. Example: git switch -c <new-branch-name>. Or to undo this operation: git switch -. You can turn off this hint by setting the configuration variable 'advice.detachedHead' to 'false'. HEAD is now at 5975930 1

(通过www.DeepL.com/Translator翻译(免费版))