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

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


Solution 1:

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

Solution 2:

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!

Solution 3:

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.

Solution 4:

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