I branched off the wrong branch in Git. How do I fix this?

February 2, 2020

You created the pull request on Github (or GitLab, or whatever) to just realize you have created your feature branch off the wrong branch.

Now is showing a lot more files than you actually changed, you will have to explain to the person who is going to review, you are worried that you could mess up Git somehow and a lot of other related thoughts are happening in your head.

Basically, (almost) anything can be fixed in Git, and fixing this kind of problem is pretty simple.

For the sake of the examples, let’s say you have 3 branches:

  • master - This is your main Git branch, the branch you should have branched off
  • feature/XX-1 - The other feature you was working, and the wrong branch you branched off
  • feature/XX-2 - The feature branch you want to create a PR and that you have created from feature/XX-1 instead of master

On either of the options below, we’ll have the same first 2 steps.

Step 1: Create a “backup” branch from our feature branch.

# make sure you are in the correct branch
git checkout feature/XX-2

# create the backup branch
git checkout -b feature/XX-2-bkp

Step 2: Re-create the feature branch from master:

git checkout master
git branch -D feature/XX-2
git checkout -b feature/XX-2

Then, we have 2 different options on how to fix our branch.

Option 1 - Use rebase –onto

git rebase --onto feature/XX-2 feature/XX-1 feature/XX-2-bkp
git push origin feature/XX-2 —force

And that’s it. If you are not sure how git rebasing works, you can check it [here](Git - Rebasing).

Option 2 - Cherry pick your commits

After executing steps 1 & 2, you only need to cherry pick your commits into the branch and force push to the origin:

git cherry-pick commit-hash-2
git cherry-pick commit-hash-2
git push origin feature/XX-2 —force

Please note that you do have to —force push since you changed the Git history compared to what’s in Github.

If you are not sure how git rebasing works, you can check it here.