GitTip: If you’re looking for how to merge a Git branch, we’ve got another page for that.
We’re going to walk through how to rebase a branch using the cross-platform GitKraken Git GUI before reviewing how to Git rebase a branch in the command line.
While other tutorials will commonly show how to Git rebase branch to master, we have chosen to use the branch name: main.
Rebasing a Git branch using the intuitive GitKraken Git client is ridiculously easy, and allows you to more clearly see what’s going on with the branches you want to rebase.
Simply drag-and-drop a branch in GitKraken onto the branch that you want as a new base, then select the Rebase <source branch name> onto <target branch name> from the context menu.
Alternatively, you can also right-click a branch from the central graph and to access the same menu option.
Unlike in the command line, where you would be required to manually update your target branch if it wasn’t up-to-date, GitKraken stays current with your remote branches for you. The process of rebase in GitKraken works exactly the same, whether or not you’re up-to-date with the base branch.
If your rebase results in conflicting files, GitKraken will immediately alert you of conflict and will present you with a tool to resolve it in-app, without context switching.
GitKraken’s merge resolution tool presents you with the conflicting files side by side, with your checked out branch on the left and your target branch on the right. There is also an output section at the bottom.
Check a box next to an individual line, or hunk, of code to add it to the output. This allows you to see your options clearly in context and make the best decision for what changes to keep, and what changes to discard.
Once the conflict has been resolved, you can move on with your rebase with the click of a button.
To rebase a Git branch using the command line, you will start by checking out the branch that contains the changes you want to rewrite onto a target branch. In this example, let’s say you want to rebase the feature branch onto the base of the main branch. You will start by running the following commands:
git checkout feature-branch
git rebase main
Git should then display a message like:
Successfully rebased and updated refs/head/feature-branch
This is telling us that the last commit on the feature-branch has been updated.
Now, if there are changes to the remote of your target branch, you will need to start by pulling the latest changes from that remote branch.
GitTip: If you’re new to working with remote branches, never fear. We’ve got everything you need to learn how to pull a remote Git branch.
In this example, your target branch is still main; you start by performing a Git pull to fetch changes from your remote.
git checkout main
git pull origin main
After the pull is completed, you move on by checking out your hotfix branch and then rebasing onto main.
git checkout hotfix
git rebase main
Sometimes, attempting to rebase a Git branch can result in conflicting changes which need to be resolved before the action can be completed. When Git detects conflicting changes, it will pause the rebase at the erroneous commit.
Unlike in GitKraken, where resolving conflicts is just one click away, you don’t have enough context to immediately identify where the conflicting code exists when working in the CLI. You will have to leave the terminal to open the conflicting files in your preferred external editor to decide which pieces of code you want to keep, and which you want to discard.
After working through and saving your changes, you can stage them by running the git add command followed by the file name. Next, you should create a commit for your resolution using git commit -mfollowed by your Git commit message.
git commit -m “my merge resolution commit message”
If the conflict has indeed been resolved, you can move on with the rebase with the following command:
git rebase --continue
Alternatively, if you’re unable to resolve the conflict, or decide you don’t wish to move forward with the rebase, you can instead use:
git rebase --abort
This will cancel the Git rebase action and your branch will be back in the state it was before starting the rebase.
Rebase Git branches with better visibility and more confidence with the cross-platform GitKraken Git GUI for Windows, Mac, & Linux.