Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a forced-push to another branch in Git

Tags:

git

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

like image 409
Ian Herve Chu Te Avatar asked Apr 29 '16 01:04

Ian Herve Chu Te


People also ask

How do I force push to a different branch?

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”.

How do I force a git push?

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

Can I force push on feature 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.


1 Answers

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.

like image 62
Jeff Puckett Avatar answered Oct 02 '22 19:10

Jeff Puckett