Reputation: 6080
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
Upvotes: 110
Views: 198847
Reputation: 758
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 >
Upvotes: 0
Reputation: 656
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 ;-)
Upvotes: 0
Reputation: 1968
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.
Upvotes: 1
Reputation: 10717
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
Upvotes: 1
Reputation: 149
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!
Upvotes: 13
Reputation: 520898
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
Upvotes: 167
Reputation: 124
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.
Upvotes: 4