111

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

3
  • 2
    Have you ensured that your commit history until #10 is what it was originally, and that only your commit #11 contains the changes? An error like that is normally given when your branch and the origin branch have a different commit history (like what happens when you re-did the dev of each of the commits) Commented May 27, 2015 at 1:22
  • 2
    I'm pretty sure they don't. I think I foolishly started doing the dev after checking out #5 Commented May 27, 2015 at 1:23
  • 2
    That's fine, what you can do is git reset [commit10Hash] to unstage your changes, then git stash to save them for a bit, git reset --hard origin/nameofremotebranch to reset it to origin, git stash pop to reapply the changes you want, then git add . and git commit -m "Your new changes on top of origin". You should then be able to git push origin nameofremotebranch without conflict Commented May 27, 2015 at 1:26

7 Answers 7

168

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 
Sign up to request clarification or add additional context in comments.

1 Comment

But doesn't this create issues for other developers who want to check out master?
13

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!

1 Comment

While a very simple answer, this is usually what fixes most of the problems leading to the OP's question. In my case, I had reverted back to an earlier commit, fixed some issues, and did a few pushes — but forgot that the HEAD was still detached! Your solution fixed everything for me. Thanks 🙏
4

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.

1 Comment

Great info - I'm actually the only one working on this right now so I think that should work
1

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 

Comments

1

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.

Comments

0

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

Comments

0

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 >

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.