Git 推送主致命: 您当前不在分支上

主人,是不是在10号房。然而,我最终意识到我在这个过程中破坏了一些测试没有发现的东西。

我最终提交了 # 5,然后慢慢地重新做了每次提交的开发,并不断地调整它,以确保它不会重新导致 bug。现在我基本上又回到了第10条,但是做了一些更改来防止 bug 的发生。

我现在想用我的改变创建提交 # 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>

这是意料之中的,但是我如何将它推送到我的远程分支呢?

我试过 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.

当我做 git status的时候,我看到的是 HEAD detached from 4a74ac3

139256 次浏览

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.

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.

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:

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:

git branch temp-branch
git checkout master
git merge temp-branch
git push origin master

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!

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.

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 ;-)

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 >