Reparent a Branch
By: Roger Creasy
I recently had several bug fixes to work on one after another. These bugs were in our development branch, and were found in testing. I branched from our development branch, fixed bugs, and made commits. Each bug was on its own branch.
When doing my PRs I realized that for one of the bugs I had branched off one of the other bug branches rather than from development. So, this one bug branch had the fixes for 2 bugs. This was not the intent. The bugs needed to be on separate branches and have separate PRs. Fortunately, with Git this was not a problem.
What I needed to do was change the branch parent to its grandparent. Assuming the branches for the bugs are named "bug1" and "bug2". I needed to change from development->bug1->bug2
to development->bug2
(development->bug1 was already separate).
The fix was easy. Below is what I did.
While one the branch bug2 run - git rebase --onto development bug1 bug2
In Git, branches are references to commits. Each of the three branches in this example represent different points in the commit history of the project.
What the command above does is move the current branch (bug2) onto the branch "development". We need a range of commits to move; that is where the last 2 arguments come in. It uses the commits from the end of "bug1" through and including "bug2". The branch "bug2" is now a fork of the branch "development".