Doing a forced push is done by:
git push origin +branch
Doing a push to a differently-named remote branch is done by:
git push origin local:remote
How does one do a forced push to a differently-named remote branch?
I tried:
git push origin local:+remote
But it creates a new branch named +remote
In order to push your branch to another remote branch, use the “git push” command and specify the remote name, the name of your local branch as the name of the remote branch. As an example, let's say that you have created a local branch named “my-feature”.
To force a push to only one branch, use a + in front of the refspec to push (e.g git push origin +master to force a push to the master branch).
Actually, force pushing to a feature branch on which only you work is not bad practice at all. --force-with-lease is safer, not safe - it's still not generally a good thing to do on a shared branch.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With