Git push master 致命:您当前不在分支上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30471557/
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 push master fatal: You are not currently on a branch
提问by Tom Hammond
Master is it at say commit #10. However, I ended up realizing I broke something along the way that wasn't caught by tests.
大师是在说提交#10。然而,我最终意识到我打破了一些没有被测试发现的东西。
I ended up going to commit #5, and then slowly re-did the dev of each commit and adjusted it continually to ensure it didn't re-cause the bug. Now I'm essentially back to commit #10, but with a number of changes that prevent the bug from happening.
我最终要提交 #5,然后慢慢地重新做每个提交的开发并不断调整它以确保它不会重新引起错误。现在我基本上回到了第 10 条提交,但做了一些更改以防止错误发生。
I now want to create commit #11 using my changes. But when I try to push to master I get
我现在想使用我的更改创建提交 #11。但是当我试图推动掌握时,我得到了
fatal: You are not currently on a branch.
To push the history leading to the current (detached HEAD)
state now, use
git push master HEAD:<name-of-remote-branch>
Which is to be expected. But how do I actually get that to push up to my remote branch?
这是可以预料的。但是我如何才能真正将其推送到我的远程分支?
I tried git push origin HEAD:master
but then got this:
我试过了,git push origin HEAD:master
但后来得到了这个:
! [rejected] HEAD -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/tomhammond/sample.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
When I do a git status
I see HEAD detached from 4a74ac3
当我做一个git status
我看到HEAD detached from 4a74ac3
回答by Tim Biegeleisen
But when I try to push to master I get
但是当我试图推动掌握时,我得到了
fatal: You are not currently on a branch.
To push the history leading to the current (detached HEAD)
fatal: You are not currently on a branch.
To push the history leading to the current (detached HEAD)
Which is to be expected
这是可以预料的
Working in a detached state is notto be expected, unless you deliberately want to be doing this, which I doubt is the case for you. Instead of checking out commit #5, you should have either reverted the master
branch to that commit, or do a git rebase
in interactive mode where you can rehash the commits as you want.
在超脱状态下工作是不可预料的,除非你故意想要这样做,我怀疑你的情况。与其检查提交 #5,您应该将master
分支恢复到该提交,或者git rebase
以交互模式执行,您可以根据需要重新散列提交。
That being said, if you are certain that the version of master
in the detached state is what you reallywant to keep, then you can get around the non-fast-forward
error, by force pushing the branch to the remote:
话虽如此,如果您确定master
处于分离状态的版本是您真正想要保留的版本,那么您可以non-fast-forward
通过强制将分支推送到远程来解决错误:
git push origin HEAD:master --force
However, if you force push you run the risk of causing problems for all other users who have that branch checked out. A less risky solution would be to create a temporary branch from the detached HEAD, and then merge that branch into master
:
但是,如果您强制推送,则可能会导致所有其他已签出该分支的用户出现问题。一个风险较小的解决方案是从分离的 HEAD 创建一个临时分支,然后将该分支合并到master
:
git branch temp-branch
git checkout master
git merge temp-branch
git push origin master
回答by Huang.SQ
have you ensured that you really in a branch? use git branch
and check if you are in a branch. if not, just git checkout branch-name-you-want
and then git push
is fine!
你确定你真的在一个分行吗?使用git branch
并检查您是否在分支中。如果没有,只是git checkout branch-name-you-want
然后git push
就好了!
回答by singron
git push
will only let you fast-forward the remote. This means the commit you are trying to push needs to be a descendent of the remote branch. Since you edited the previous commits after 5, you don't have a descendent but more of a cousin. You can give git push --force
if you want to overwrite the branch, but if other people have made their own changes on top of the current master, they won't be able to pull the branch anymore. Also, if someone else pushes to master before you do, their changes will be lost. Generally, you don't want to force push if you are not the only one using a branch.
git push
只会让你快进遥控器。这意味着您尝试推送的提交需要是远程分支的后代。由于您在 5 之后编辑了之前的提交,因此您没有后代,而是更多的堂兄弟。git push --force
如果你想覆盖分支,你可以给,但如果其他人在当前 master 之上进行了自己的更改,他们将无法再拉分支。此外,如果其他人在您之前推动掌握,他们的更改将丢失。一般来说,如果你不是唯一一个使用分支的人,你不想强制推送。