This guide shows you how you can create a new branch with the original changes of your broken branch.
Problems when merging or rebasing branches in a version control system like git can happen to anyone. I want to show you one way that works pretty good to resolve the issue. Of course, there are other ways like reverting the rebase and doing it again, but I like this approach more.
Relevant git commands
▶ git cherry-pick
▶ git log
▶ git checkout
▶ git branch
1 Create a new branch from the merge/rebase target
Let’s assume you wanted to rebase the main
branch onto your dev
branch and something went wrong because the application does launch anymore.
So you create a new branch dev2
from the main
branch as the first step. That means you have all the latest changes from main
already in your branch and you don’t need to rebase anymore.
2 Apply the changes from the broken branch to the new branch
The next step is to apply all your changes from dev
to dev2
. Switch to your dev2
branch and get all commits with the git cherry-pick <id>
command.
To find the commit ids, you can use the git log
command on the dev
branch (see image below). Apply the changes in chronological order. In the end, you have a branch with all of your changes and also the changes of the rebase source.
3 Push the new branch
Finally, push the dev2
branch. You might need to set the upstream, but git will assist you with that.
4 Delete the broken branch
The original dev
branch is useless now. You can delete it in your local repository as well as in the remote repository.
Conclusion
I showed you a strategy to fix problems with a broken branch by replacing it with a working one. It’s a simple and straightforward approach and might help you in the future.