Git:“目前不在任何分支上。” 有没有一种简单的方法可以在保留更改的同时返回分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4735556/
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
Git: "Not currently on any branch." Is there an easy way to get back on a branch, while keeping the changes?
提问by Erik B
So I've done some work in the repository and when I'm about to commit I realize that I'm not currently on any branch.
所以我在存储库中做了一些工作,当我准备提交时,我意识到我目前不在任何分支上。
This happens a lot when working with submodules and I am able to solve it, but the process is tedious and I've been thinking that there must be an easier way to do this.
这种情况在使用子模块时经常发生,我能够解决它,但这个过程很乏味,我一直认为必须有一种更简单的方法来做到这一点。
Is there an easy way to get back on a branch, while keeping the changes?
有没有一种简单的方法可以在保留更改的同时返回分支?
回答by araqnid
If you have not committed:
如果您还没有承诺:
git stash
git checkout some-branch
git stash pop
If you have committed and have not changed anything since:
如果您已承诺并且此后没有更改任何内容:
git log --oneline -n1 # this will give you the SHA
git checkout some-branch
git merge ${commit-sha}
If you have committed and then done extra work:
如果你已经提交然后做了额外的工作:
git stash
git log --oneline -n1 # this will give you the SHA
git checkout some-branch
git merge ${commit-sha}
git stash pop
回答by babay
this helped me
这对我有帮助
git checkout -b newbranch
git checkout master
git merge newbranch
git branch -d newbranch
回答by m_messiah
git checkout master
That's result something like this:
结果是这样的:
Warning: you are leaving 2 commits behind, not connected to
any of your branches:
1e7822f readme
0116b5b returned to clean django
If you want to keep them by creating a new branch, this may be a good time to do so with:
git branch new_branch_name 1e7822f25e376d6a1182bb86a0adf3a774920e1e
So, let's do it:
所以,让我们这样做:
git merge 1e7822f25e376d6a1182bb86a0adf3a774920e1e
回答by Observer
Leaving another way here
离开这里的另一条路
git branch newbranch
git checkout master
git merge newbranch
回答by Abizern
Alternatively, you could setup your submodules so that rather than being in their default detached head state you check out a branch.
或者,您可以设置您的子模块,以便您签出一个分支,而不是处于默认的分离头状态。
Edited to add:
编辑添加:
One way is to checkout a particular branch of the submodule when you add it with the -b flag:
一种方法是在使用 -b 标志添加子模块时检出它的特定分支:
git submodule add -b master <remote-repo> <path-to-add-it-to>
Another way is to just go into the submodule directory and just check it out
另一种方法是进入子模块目录并查看它
git checkout master
回答by Erik B
I recently ran into this problem again. It's been a while since I last worked with submodules and having learned more about git I realized that simply checking out the branch you want to commit on is sufficient. Git will keep the working tree even if you don't stash it.
我最近又遇到了这个问题。自从我上次使用子模块并了解了有关 git 的更多信息以来,已经有一段时间了,我意识到只需检查您想要提交的分支就足够了。即使您不隐藏它,Git 也会保留工作树。
git checkout existing_branch_name
If you want to work on a new branch this should work for you:
如果你想在一个新的分支上工作,这应该适合你:
git checkout -b new_branch_name
The checkout will fail if you have conflicts in the working tree, but that should be quite unusual and if it happens you can just stash it, pop it and resolve the conflict.
如果工作树中存在冲突,结帐将失败,但这应该是非常不寻常的,如果发生这种情况,您可以将它隐藏起来,弹出它并解决冲突。
Compared to the accepted answer, this answer will save you the execution of two commands, that don't really take that long to execute anyway. Therefore I will not accept this answer, unless it miraculously gets more upvotes (or at least close) than the currently accepted answer.
与接受的答案相比,这个答案将为您节省两个命令的执行时间,无论如何都不需要那么长时间来执行。因此,我不会接受这个答案,除非它奇迹般地获得比当前接受的答案更多的赞成(或至少接近)。
回答by Ian
One way to end up in this situation is after doing a rebase from a remote branch. In this case, the new commits are pointed to by HEAD
but master
does not point to them -- it's pointing to wherever it was before you rebased the other branch.
导致这种情况的一种方法是在从远程分支执行 rebase 之后。在这种情况下,新提交被指向HEAD
但master
不指向它们——它指向你重新定位另一个分支之前的任何地方。
You can make this commit your new master
by doing:
您可以master
通过执行以下操作使此提交成为您的新内容:
git branch -f master HEAD
git checkout master
This forcibly updates master
to point to HEAD
(without putting you on master
) then switches to master
.
这会强制更新master
为指向HEAD
(不让您穿上master
)然后切换到master
.
回答by kenorb
The following method may work:
以下方法可能有效:
git rebase HEAD master
git checkout master
This will rebase your current HEAD changes on top of the master. Then you can switch the branch.
这将使您当前的 HEAD 更改基于母版。然后就可以切换分支了。
Alternative way is to checkout the branch first:
另一种方法是先检出分支:
git checkout master
Then Git should display SHA1 of your detached commits, then you can cherry pick them, e.g.
然后 Git 应该显示你分离提交的 SHA1,然后你可以挑选它们,例如
git cherry-pick YOURSHA1
Or you can also merge the latest one:
或者你也可以合并最新的:
git merge YOURSHA1
To see all of your commits from different branches (to make sure you've them), run: git reflog
.
要查看所有提交来自不同分支的(以确保您将他们),运行:git reflog
。
回答by Erik B
I know I told babay in 2012 that I thought it was unlikely that someone wouldn't realize that they weren't on a branch and commit. This just happened to me, so I guess I have to admit that I was wrong, but considering that it took until 2016 for this to happen to me, you could argue that it is in fact unlikely.
我知道我在 2012 年告诉 babay,我认为有人不会意识到他们不在分支上并提交的可能性很小。这只是发生在我身上,所以我想我不得不承认我错了,但考虑到这件事发生在 2016 年之前,你可能会争辩说这实际上不太可能。
Anyway, creating a new branch is overkill in my opinion. All you have to do is:
无论如何,在我看来,创建一个新分支是多余的。您所要做的就是:
git checkout some-branch
git merge commit-sha
If you didn't copy the commit-sha before checking out the other branch, you can easily find it by running:
如果您在检出另一个分支之前没有复制 commit-sha,您可以通过运行轻松找到它:
git reflog