How to do a forced-push to another branch in Git
The +
needs to come at the beginning of the argument representing the couple.
git push origin +localBranchName:remoteBranchName
That's hard to remember sometimes, so there's also the --force
flag.
git push origin --force localBranchName:remoteBranchName
But be aware if you push multiple branches with that flag, then they will all be force pushed.
git push origin --force localBranchName:remoteBranchName anotherLocalBranch
In that case, you may not have wanted to force push anotherLocalBranch
, so you should instead use the +
to specify which ones you want forced.
git push origin +localBranchNameForced:remoteBranchName localBranchNotForced:remoteBranchNotForced +anotherLocalBranchForcePushedToUpstreamTracking
Do read Torek's answer for a better explanation, and check out some of his other answers for world-class knowledge on git.
Some people are not reading Jeff Puckett's answer first. Do that! This answer is about the use of the +
force-push character, not about force-pushing in general.
Just to be a bit more complete than the accepted answer: the syntax for a refspec is [+][src][:dst]
, with at most one of src
and :dst
being omitted (so that the empty string and +
by itself are both invalid).
Hence the non---force
syntax for your case is +local:remote
, rather than local:+remote
.
Using --force
has the effect of adding the +
in front of every refspec, i.e., these two are equivalent:
git push --force origin someBranch local:remote anotherBranch
git push origin +someBranch +local:remote +anotherBranch