Git:“当前不在任何分支上。”有没有一种简单的方法可以在保留更改的同时返回分支?
因此,我已经在存储库中完成了一些工作,当我即将提交时,我意识到我当前不在任何分支上。
在使用子模块时,这种情况经常发生,我能够解决它,但这个过程很乏味,我一直在想必须有一种更简单的方法来做到这一点。
有没有一种简单的方法可以在保留更改的同时返回分支?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果您尚未承诺:
如果您已承诺并且此后没有更改任何内容:
如果您已承诺并完成了额外工作:
If you have not committed:
If you have committed and have not changed anything since:
If you have committed and then done extra work:
这对我有帮助
this helped me
结果是这样的:
所以,让我们这样做:
That's result something like this:
So, let's do it:
出现这种情况的一种方法是从远程分支执行变基操作。在这种情况下,新的提交由
HEAD
指向,但master
并不指向它们——它指向您重新设置另一个分支之前的位置。您可以通过以下方式提交您的新
master
:这会强制更新
master
以指向HEAD
(而不会将您置于master< /code>) 然后切换到
master
。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
butmaster
does not point to them -- it's pointing to wherever it was before you rebased the other branch.You can make this commit your new
master
by doing:This forcibly updates
master
to point toHEAD
(without putting you onmaster
) then switches tomaster
.这里另留一条路
Leaving another way here
或者,您可以设置子模块,以便您签出分支,而不是处于默认的分离头状态。
编辑添加:
一种方法是在使用 -b 标志添加子模块时签出子模块的特定分支:
另一种方法是进入子模块目录并签出它
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:
Another way is to just go into the submodule directory and just check it out
我最近又遇到了这个问题。自从我上次使用子模块并且了解了有关 git 的更多信息以来已经有一段时间了,我意识到只需检查您想要提交的分支就足够了。即使您不隐藏工作树,Git 也会保留它。
如果您想在新分支上工作,这应该适合您:
如果工作树中存在冲突,则签出将会失败,但这应该是很不寻常的,如果发生这种情况,您可以将其隐藏,弹出并解决冲突。
与接受的答案相比,这个答案将为您节省执行两个命令的时间,无论如何,这两个命令实际上不会花费那么长时间来执行。因此,我不会接受这个答案,除非它奇迹般地获得比当前接受的答案更多的赞成票(或至少接近)。
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.
If you want to work on a new branch this should work for you:
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.
以下方法可能有效:
这会将您当前的 HEAD 更改基于 master 之上。然后就可以切换分支了。
另一种方法是首先签出分支:
然后 Git 应该显示分离提交的 SHA1,然后您可以挑选它们,例如
或者您也可以合并最新的:
查看来自不同分支的所有提交(以确保您已经有了),运行:
git reflog
。The following method may work:
This will rebase your current HEAD changes on top of the master. Then you can switch the branch.
Alternative way is to checkout the branch first:
Then Git should display SHA1 of your detached commits, then you can cherry pick them, e.g.
Or you can also merge the latest one:
To see all of your commits from different branches (to make sure you've them), run:
git reflog
.我知道我在 2012 年告诉 babay,我认为有人不太可能没有意识到他们不在分支上并提交。这件事就发生在我身上,所以我想我必须承认我错了,但考虑到直到 2016 年这件事才发生在我身上,你可能会说这实际上不太可能。
无论如何,在我看来,创建一个新分支是矫枉过正的。您所要做的就是:
如果您在检查其他分支之前没有复制 commit-sha,您可以通过运行以下命令轻松找到它:
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.
Anyway, creating a new branch is overkill in my opinion. All you have to do is:
If you didn't copy the commit-sha before checking out the other branch, you can easily find it by running:
我发现的最好方法是将已更改的文件复制到单独的文件夹中,
然后删除我计算机上当前的存储库文件夹,
然后克隆主存储库并将文件放回去。
The best way that I found was to copy the files that I have made changes to to a separate folder,
then delete the repository folder I currently have on my computer,
then clone the main repository and put the files back.