Manuel Schiller Manuel Schiller - 2 months ago 42
Git Question

How to overwrite remote branch with different local branch with 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

Answer

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
Comments