简体   繁体   English

Git push master fatal: 你目前不在分支

[英]Git push master fatal: You are not currently on a branch

Master is it at say commit #10. Master 是在 commit #10 处。 However, I ended up realizing I broke something along the way that wasn't caught by tests.然而,我最终意识到我在测试过程中破坏了一些没有被测试发现的东西。

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.我最终提交了#5,然后慢慢地重新做了每个提交的开发并不断调整它以确保它不会重新引起错误。 Now I'm essentially back to commit #10, but with a number of changes that prevent the bug from happening.现在我基本上回到了提交 #10,但是有一些更改可以防止错误发生。

I now want to create commit #11 using my changes.我现在想使用我的更改创建提交 #11。 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)
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

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)

Which is to be expected这是可以预料的

Working in a detached state is not to 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.您应该将master分支恢复到该提交,而不是检查提交 #5,或者在交互模式下执行git rebase ,您可以根据需要重新哈希提交。

That being said, if you are certain that the version of master in the detached state is what you really want 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

git push will only let you fast-forward the remote. git push只会让你快进遥控器。 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.由于您在 5 之后编辑了之前的提交,因此您没有后代,而是更多的堂兄弟。 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.如果你想覆盖分支,你可以给git push --force ,但是如果其他人在当前 master 之上进行了自己的更改,他们将无法再拉分支。 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.一般来说,如果你不是唯一一个使用分支的人,你不想强制推送。

have you ensured that you really in a branch?你确定你真的在一个分行吗? use git branch and check if you are in a branch.使用git branch并检查您是否在分支中。 if not, just git checkout branch-name-you-want and then git push is fine!如果没有,只需git checkout branch-name-you-want然后git push就可以了!

You can create a new branch and you can merge these changes in the previous branch您可以创建一个新分支,并且可以将这些更改合并到之前的分支中

git checkout -b newBranch 
git checkout previousBranch
git merge newBranch
git push origin previousBranch
git branch -d previousBranch

If you are pushing to a new repository you can use如果您要推送到新的存储库,您可以使用

git push origin HEAD:refs/heads/main --force

main here is a new branch that will be created in case your target repository is empty.这里的main是一个新分支,如果您的目标存储库为空,将创建该分支。

This one should work if you want do it without rebase如果你想在没有变基的情况下这样做,这个应该可以工作

git config pull.rebase false 
git pull origin master
git add .
git commit -m "Your Commit message"
git push

Now your good to go;-)现在您对 go 有好处;-)

You can create a new branch to retain the commits you created and then push them to the remote.您可以创建一个新分支来保留您创建的提交,然后将它们推送到远程。

Use this command:使用此命令:

git switch -c < new-branch-name > git switch -c <新分支名称>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM