DEV Community

Cover image for Git rebase a remote branch in the terminal
Shehroz Irfan
Shehroz Irfan

Posted on

Git rebase a remote branch in the terminal

Hi,
In this article, you will learn how to git rebase a remote branch in the terminal. I am dividing the whole process of rebasing into 5 steps. Let's understand this with the example below:

You are on a branch named feature and you want to rebase this branch onto the branch named main.

Step 1: Pull the latest changes from the remote of your target branch.

In the example, the target branch(the branch onto which you want to rebase the feature branch) is main.
As we are currently on feature, so first we need to checkout to the main and then we will pull the latest changes.

git checkout main
Enter fullscreen mode Exit fullscreen mode
git pull origin main
Enter fullscreen mode Exit fullscreen mode

Step 2: Rebasing feature onto main.


When the pull is completed, checkout out to feature and then rebase feature onto main.

git checkout feature
Enter fullscreen mode Exit fullscreen mode
git rebase main
Enter fullscreen mode Exit fullscreen mode

Step 3: Resolving Conflict

Sometimes, rebasing a Git branch can result in conflicting changes that need to be resolved before the action can be completed.

When there is a conflict in a file it looks like this:

Image description

In the above image you can see that Git adds some additional lines to the file having conflict:

  • <<<<<<< HEAD
  • =======
  • >>>>>>> updated_address

You can think of ======= as the dividing line of the conflict. Everything between <<<<<<< HEAD and ======= is the content of the current branch that the HEAD ref is pointing to, also called as Current Change. On the other hand, everything between ======= and >>>>>>> updated_address is the content in the branch being merged, also called as Incoming Change.

After resolving the conflict and saving your changes, you need to tell Git that the conflict has been resolved, you can do this with:

git add <filename>
Enter fullscreen mode Exit fullscreen mode

Step 4: Rebase Continue

Now you can move on with the rebase by:

git rebase --continue
Enter fullscreen mode Exit fullscreen mode

So, when the rebase is completed. Your branch feature is up-to-date with all changes which had been previously committed to the main branch.

Alternatively, if you’re unable to resolve the conflicts, or decide you don’t want to move forward with the rebase, then you can cancel the Git rebase action, with:

git rebase --abort
Enter fullscreen mode Exit fullscreen mode

Now your branch is back in the state, as it was in before starting the rebase.

Step 5: Pushing rebased code to GitHub

As you've altered Git history, the usual git push origin will not work. You'll need to modify the command by "force-pushing" your latest changes:

git push origin feature -f
Enter fullscreen mode Exit fullscreen mode

Discussion (4)

Collapse
khawajamujaddid profile image
khawajaMujaddid

I was facing a problem merging two branches on which I was working on, This article
really helped me remove the conflicts. Keep up the good and hope to see more informational articles from your side.

Collapse
shehrozirfan profile image
Shehroz Irfan Author

Thanks.

Collapse
moeznazir profile image
Moez Nazir

Excellent. Got my concept of git rebase cleared now!

Collapse
shehrozirfan profile image
Shehroz Irfan Author

Thanks.