MichelReap MichelReap - 2 months ago 6
Git Question

Change local branch to a different one

I was working on a

release/1.0
branch and I started working on some changes. Unadvertedly I commited these changes to the same branch I was in, that is
release/1.0


Now this branch has locally a few changes that are not on the remote
release/1.0
branch.

Turns out our repo system does not allow for pushing changes directly to release branches, but we should create pull requests instead.

So now I have my
bugfix/my-branch
(branching remote
release/1.0
) where I would like the changes that are locally on release/1.0 to be.

How can I "move" those changes to this new branch and discard them locally for
release/1.0
?

Answer

You might want to try a git rebase.


Optional Pre-Step:
If you are not sure (or don't know) which branch your bugfix should be based on do the following (because you mentioned that you already have a bugfix/my-branch branch.

$ git branch -m bugfix/my-branch starting-point

First name your branch as what it really is:

$ git branch -m bugfix/my-branch

Then rebase it into the correct position (assuming starting-point)

$ git rebase --onto starting-point release/1.0 bugfix/my-branch

This will move everything between release/1.0 and bugfix/my-branch (all the commits you did) onto the brach were you should have branched off.


And after everything went well you can safely delete the starting-point branch.

$ git branch -d starting-point
Comments