Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overwrite remote branch with different local branch with git

Tags:

git

I have a remote branch that is the basis for a pullrequest.

I mainly worked on a different branch, however that should now replace the old branch.

I tried to do a git push remote oldBranch -f but that only pushes my latest local oldBranch to the git server instead of the current branch - no matter which branch i am currently on.

How can I replace the remote branch with my local branch?

EDIT: If anybody else is interested, this is how i got this to work:

git checkout oldBranch git branch -m 'oldBranchToBeReplaced' git checkout newBranch git branch -m oldBranch git push myrepo oldBranch -f 
like image 883
Manuel Schiller Avatar asked Sep 08 '16 11:09

Manuel Schiller


People also ask

How do you overwrite a remote?

What you basically want to do is to force push your local branch, in order to overwrite the remote one. If you want a more detailed explanation of each command, then see my long answers section below. Warning: force pushing will overwrite the remote branch with the state of the branch that you're pushing.

How do I change my remote to another branch?

In order to switch to a remote branch, make sure to fetch your remote branch with “git fetch” first. You can then switch to it by executing “git checkout” with the “-t” option and the name of the branch.


1 Answers

You can use the local-name:remote-name syntax for git push:

git push origin newBranch:oldBranch 

This pushes newBranch, but using the name oldBranch on origin.

Because oldBranch probably already exists, you have to force it:

git push origin +newBranch:oldBranch 

(I prefer the + instead of -f, but -f will work, too)

To delete a branch on the remote side, push an "empty branch" like so:

git push origin :deleteMe 
like image 99
Martin Nyolt Avatar answered Oct 30 '22 02:10

Martin Nyolt