git 如何将我的提交从“无分支”“移动”到实际分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8953206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:48:01  来源:igfitidea点击:

How do I "move" my commits from "no branch" to an actual branch?

gitmergebranchcommitgit-branch

提问by Letharion

I made a mistake, and started making commits "to the last tag", which lands my commits in "no branch". They should have been applied at the head of an already existing branch. I have notpushed my changes yet. I've found a few other question for when commits are in the wrongbranch, but now I have nobranch, so I don't know how to handle this.

我犯了一个错误,开始提交“到最后一个标签”,这使我的提交落在“无分支”中。它们应该已应用于现有分支的负责人。我还没有推送我的更改。我发现了其他一些关于何时提交位于错误分支的问题,但现在我没有分支,所以我不知道如何处理。

Preferably, I would like to erase my mistake entirely, and "move" my changes to the end of the right branch. If I must leave my mistake in the history, I need to merge them in atleast.

最好,我想完全消除我的错误,并将我的更改“移动”到正确分支的末尾。如果我必须将我的错误留在历史中,我至少需要将它们合并。

回答by drrlvn

You are currently in a detached HEADstate. To resolve that, all you need to do is create a new branch with git branch <branchname>or git checkout -b <branchname>. That will leave you with a local branch you can play with, and even delete when you're done with it.

您当前处于分离的 HEAD状态。要解决这个问题,您需要做的就是使用git branch <branchname>或创建一个新分支git checkout -b <branchname>。这会给你留下一个你可以玩的本地分支,甚至在你完成后删除。

Branches in git are just pointersto commits, so if you create a new branch where you are the new branch will point to your current commit, and then you can merge it or whatnot.

git 中的分支只是指向提交的指针,因此如果您创建一个新分支,您所在的新分支将指向您当前的提交,然后您可以合并它或诸如此类。

Your "mistake" need notbe erased, you simply created new commits on top of previous ones. You did not modify history or anything like that.

您的“错误”需要被删除,您只需在以前的之上创建新的提交。你没有修改历史或类似的东西。

EDIT: In response to your comment, what you need to do is:

编辑:为了回应您的评论,您需要做的是:

git branch temp
git checkout master # or any other branch
git merge temp
git branch -d temp

回答by Sailesh

You can view all your commits using git reflog

您可以使用查看所有提交 git reflog

So you can simply got to another branch, and do git cherry-pick <commit-hash>for the required commits.

所以你可以简单地进入另一个分支,并git cherry-pick <commit-hash>为所需的提交做。

But I'd prefer the branch way as spatzmentioned.

但我更喜欢spatz提到的分支方式。

回答by VonC

Note: you have also

注意:你也有

enter image description here

在此处输入图片说明

In both case, doing a tmpbranch and merging it back to the actual branch is the solution.

在这两种情况下,执行tmp分支并将其合并回实际分支是解决方案。

回答by Nathan Buesgens

Another solution, which does not involve creating a temp branch, is described here. You just merge with you last commit rather than a temp branch.

此处描述另一种解决方案,它不涉及创建临时分支。您只需与上次提交合并而不是临时分支。

$ git checkout master
$ git merge d2bdb98

If you don't know what commit you are on you can find it with git log. I don't know if this solution is different than "cherry-picking" but it had the expected results for me.

如果你不知道你在做什么提交,你可以用git log. 我不知道这个解决方案是否与“樱桃采摘”不同,但它对我来说有预期的结果。

回答by mpontillo

I just had a situation where I had a masterbranch I wanted to commit to, but accidentally committed to a detached HEAD(note, the commit hash is not important here, but just to illustrate the situation):

我刚刚遇到了一种情况,我有一个master我想提交的分支,但不小心提交到了一个分离的分支HEAD(注意,提交哈希在这里并不重要,只是为了说明这种情况):

$ git branch
* (detached from 29d2cfb)
  master

Here was my quick fix:

这是我的快速修复:

$ git branch -f master HEAD
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Piece of cake.

小菜一碟。

Note that this trick only works if masteroriginally pointed to the same commit you made your mistaken commit on top of. If that's not the case, you'll have to rebase (or cherry-pick, or merge...).

请注意,此技巧仅在master最初指向您错误提交的同一个提交时才有效。如果不是这种情况,您将不得不重新设定基准(或挑选或合并...)。