I have forked a github repository and by now my fork is several commits ahead. I now want to provide one of these commits as a PR to the original repository.
Following this question’s answer, I did:
git remote add official [URL to original repo]
git checkout -b hotfix-for-feature official/master
git cherry-pick [feature-hash]
git push -u origin hotfix-for-feature
and I get:
! [rejected] hotfix-for-feature -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/myusername/repositoryname'
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.
Note how:
- The branch argument to
git push -uis just completely ignored and it wants to push to origin/master - Neither
origin/hotfix-for-featurenorofficial/hotfix-for-featureexist remotely. official/masteris not ahead in any way – I literally just fetched it
If I try git branch hotfix-for-feature --set-upstream=origin/hotfix-for-feature I’m told that git push -u is the proper way to do this. If I try git push -u origin/hotfix-for-feature I get:
fatal: You are pushing to remote 'origin/hotfix-for-feature',
which is not the upstream of your current branch 'hotfix-for-feature',
without telling me what to push to update which remote branch.
which – apart from being a good example of how not to write your error messages – I don’t understand. I specify what to push (current branch) and which remote branch to update.
I find lots of questions about this error, but it is always about some remote branch being ahead and in this case there is no existing remote branch. Also what is the difference between a "pushed branch" and a "remote [branch]"?
>Solution :
You got the following error message:
! [rejected] hotfix-for-feature -> master (non-fast-forward) error: failed to push some refs to 'https://github.com/myusername/repositoryname' 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.
The important part of that is
hotfix-for-feature -> master
You are trying to push the local branch hotfix-for-feature to the remote branch master.
The branch argument to git push -u is just completely ignored and it wants to push to origin/master
It is not ignored. It pushes the local branch hotfix-for-feature to the remote branch master.
The doculentation of git push meantions the use of a refspec:
Specify what destination ref to update with what source object. The format of a parameter is an optional plus
+, followed by the source object , followed by a colon:, followed by the destination ref .…
In the second argument, you can not only specify what (local) branch to push but also what (remote) branch to push to (git push <remote> <sourcebranch>:<destinationbranch>).
Knowing this, you can just use git push -u origin hotfix-for-feature:hotfix-for-feature for pushing the local branch hotfix-for-feature to the remote branch hotfix-for-feature.