Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

github

Master is it at say 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. Now I'm essentially back to commit #10, but with a number of changes that prevent the bug from happening.

I now want to create commit #11 using my changes. 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:

! [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

like image 574
Tom Hammond Avatar asked May 27 '15 01:05

Tom Hammond


People also ask

How do I push to a branch?

Push a new Git branch to a remote repo Clone the remote Git repo locally. Create a new branch with the branch, switch or checkout commands. Perform a git push with the –set-upstream option to set the remote repo for the new branch. Continue to perform Git commits locally on the new branch.

How do I fix the detached head at origin master?

All you have to do is 'git checkout [branch-name]' where [branch-name] is the name of the original branch from which you got into a detached head state. The (detached from asdfasdf) will disappear. Show activity on this post. And head is re attached!

How do I reset my head?

To hard reset files to HEAD on Git, use the “git reset” command with the “–hard” option and specify the HEAD. The purpose of the “git reset” command is to move the current HEAD to the commit specified (in this case, the HEAD itself, one commit before HEAD and so on).

Do you want to commit to a detached head?

The "Detached Head" serves as a warning that you may also want to create or point to a branch if you intend to do any work from that point. But If you simply wish to view that tag or commit, there is nothing wrong with being in a detached head state.


2 Answers

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
like image 197
Tim Biegeleisen Avatar answered Oct 13 '22 12:10

Tim Biegeleisen


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!

like image 9
Huang.SQ Avatar answered Oct 13 '22 12:10

Huang.SQ