为什么 git 将我们设置在(没有分支)上?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2498458/
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
Why did git set us on (no branch)?
提问by e-satis
This morning we pull from our repo, and git put us on (no branch).
今天早上我们从我们的仓库中拉出来,git 把我们放在了(没有分支)上。
I don't understand this, why did this happen? And how get out of it without losing our changes?
我不明白,为什么会这样?以及如何在不丢失我们的更改的情况下摆脱它?
回答by Josh Lee
"Not currently on any branch" means you have a detachedhead, i.e. your HEAD pointer is directly referencing a commit instead of symbolically pointing at the name of a branch.
“当前不在任何分支上”意味着您有一个分离的head,即您的 HEAD 指针直接引用提交而不是象征性地指向分支的名称。
You can get into this situation by checking out a commit by SHA1, or when you're in the middle of a rebase, or when a merge fails. It's hard to say what you may have done to get into this situation by accident.
您可以通过检查 SHA1 提交的提交,或者当您处于 rebase 中时,或者当合并失败时进入这种情况。很难说你做了什么意外进入这种情况。
It's said that you may lose your changes when you switch from a detached HEAD to some branch, but the reflog will always keep track of where your HEAD moved. In fact, Git 1.7.5 will warn you when switching from a detached HEAD will lose commits. The only time you can reallylose work is when you have uncommitted changes, which you may want to commit or stash.
据说当您从分离的 HEAD 切换到某个分支时,您可能会丢失更改,但 reflog 将始终跟踪您的 HEAD 移动的位置。事实上,当从分离的 HEAD 切换将丢失提交时,Git 1.7.5 会警告你。唯一可能真正丢失工作的时间是当您有未提交的更改时,您可能想要提交或隐藏这些更改。
A simple way to see what happened is git reflog
or git log -g --decorate
for a more verbose listing. The --decorate
option will label every SHA1 with the names of all the branches that point at it. If the SHA1 of your current HEAD is exactly the same as master, then you don't have to do anything but git checkout master
to get back on track. Otherwise, see if the SHA1 is pointed to by some other branch. If not, you may wish to create a branch to hang on to it.
查看发生了什么的一种简单方法是git reflog
或git log -g --decorate
更详细的列表。该--decorate
选项将用指向它的所有分支的名称标记每个 SHA1。如果您当前 HEAD 的 SHA1 与 master 完全相同,那么您无需做任何事情,git checkout master
只需回到正轨即可。否则,查看其他分支是否指向 SHA1。如果没有,您可能希望创建一个分支来挂在它上面。
Another good command is git branch -av
, which will similarly list all branches and what they point to, so you can see what your (no branch)
is really supposed to be.
另一个很好的命令是git branch -av
,它同样会列出所有分支及其指向的内容,因此您可以看到(no branch)
真正应该是什么。
回答by Chris Johnsen
It is hard to tell without more details.
没有更多细节就很难说。
git pull
fetches changes from the remote repository and then does a merge. It can be configured to do a rebase instead of a merge (either with by doing git pull --rebase
, or by configuring a true value for branch.<branch_name>.rebase
for the branch into which you are pulling).
git pull
从远程存储库获取更改,然后进行合并。它可以配置为执行变基而不是合并(通过执行git pull --rebase
,或通过为branch.<branch_name>.rebase
要拉入的分支配置真值)。
If you started out on a branch, any merge-type pull will always leave you on that branch. On the other hand, the rebase command always works by using a temporarily detached HEAD (aka “no branch”). If you were left in this state during a rebase-type pull, then it is because the rebase portion of the pull encountered conflicts and it is waiting for you to resolve them and use rebase --continue
(or --skip
, or --abort
).
如果你从一个分支开始,任何合并类型的 pull 都会让你留在那个分支上。另一方面,rebase 命令总是通过使用临时分离的 HEAD(又名“无分支”)来工作。如果您在 rebase 类型的 pull 期间处于这种状态,那么这是因为 pull 的 rebase 部分遇到了冲突,它正在等待您解决它们并使用rebase --continue
(或--skip
、 或--abort
)。
By default, the reflog stores every update that is made to HEAD (there can also be one for each branch). You can view the reflog with git reflog show
(or git log -g
for a more verbose view). It might help you determine how you got into this state.
默认情况下,reflog 存储对 HEAD 所做的每个更新(每个分支也可以有一个)。您可以使用git reflog show
(或git log -g
更详细的视图)查看引用日志。它可能会帮助您确定您是如何进入这种状态的。
If you are in the middle of a rebase (you have a .git/rebase-apply
directory), then it probably stopped to let you resolve some conflict(s). Use git status
to check for “unmerged” entries. Any such entries should have conflict markers embedded in the files (assuming that they are plain text files). You should edit them to resolve the conflict(s) and them mark them as merged by running git add
on them. Then run git rebase --continue
to continue with the rebase. You may run into more conflicts that should be handled in a similar way (edit, add, continue). If you decide that you no longer need a particular commit, you can skip it with git rebase --skip
. You can abort the whole rebase with git rebase --abort
. All of these rebase commands are listed in the error message when a rebase stops due to any conflict. Once all the pending commits have been applied (or skipped), your original branch will be updated with the final new commit and your HEAD will be reattached to it (if you abort, your HEAD will be reattached without updating the branch).
如果您正处于变基中(您有一个.git/rebase-apply
目录),那么它可能会停止让您解决一些冲突。使用git status
检查“未合并”的条目。任何此类条目都应在文件中嵌入冲突标记(假设它们是纯文本文件)。您应该编辑它们以解决冲突,并通过运行它们将它们标记为已合并git add
。然后运行git rebase --continue
以继续 rebase。您可能会遇到更多应该以类似方式处理的冲突(编辑、添加、继续)。如果您决定不再需要某个特定提交,则可以使用git rebase --skip
. 您可以中止整个变基git rebase --abort
. 当 rebase 由于任何冲突而停止时,所有这些 rebase 命令都列在错误消息中。一旦应用(或跳过)所有挂起的提交,您的原始分支将使用最终的新提交更新,并且您的 HEAD 将重新附加到它(如果您中止,您的 HEAD 将重新附加而不更新分支)。
If your detached HEAD is not due to conflicts incurred in the middle of a rebase, then your HEAD was detached at some point prior to the pull. You will need to evaluate the current state of the tree to decide what you want to do. You might use git show-branch --current --all
or git log --graph --oneline --decorate --all
or a graphical tool like gitk
to find out how your current (detached) HEAD relates to your other branches. If you decide that you want to keep the contents of your HEAD, then you can you can make a new branch for them with git branch new_branch_name
. If you want to overwrite an existing branch, then use git branch --force existing_branch_name
. Then use git checkout branch_name
to reattach your repository's HEAD to a branch.
如果您分离的 HEAD 不是由于 rebase 中间发生的冲突,那么您的 HEAD 在拉动之前的某个时间点分离。您将需要评估树的当前状态以决定您想要做什么。您可能会使用git show-branch --current --all
或git log --graph --oneline --decorate --all
或图形工具gitk
来了解当前(分离的)HEAD 与其他分支的关系。如果您决定要保留 HEAD 的内容,那么您可以使用git branch new_branch_name
. 如果要覆盖现有分支,请使用git branch --force existing_branch_name
. 然后用于git checkout branch_name
将存储库的 HEAD 重新附加到分支。
回答by VonC
Note that, in case of a "git pull --rebase
" run while the HEAD
is detached, Git tried to find the upstream branch of the detached HEAD
(which by definition does not exist) and emitted unnecessary error messages.
请注意,如果git pull --rebase
在HEAD
分离时运行“ ” ,Git 会尝试找到分离的上游分支HEAD
(根据定义,该分支不存在)并发出不必要的错误消息。
This is no longer the case with Git1.8.0.1 (26th November 2012)
Git1.8.0.1 不再是这种情况(2012 年 11 月 26 日)
See this commit.
看到这个提交。