How to Git Rebase Origin/Branch vs. Git Rebase Origin Branch

John Wachira Feb 02, 2024
  1. Difference Between the git rebase origin/branch vs. git rebase origin master Commands
  2. Conclusion
How to Git Rebase Origin/Branch vs. Git Rebase Origin Branch

This article discusses the difference between the git rebase origin/branch and git rebase origin branch commands. We use both commands to rebase from the remote, but practical uses vary, as we will discuss shortly.

Difference Between the git rebase origin/branch vs. git rebase origin master Commands

To get the difference between the two commands, let’s look at what each command does and what workflow they follow.

the git rebase origin/branch Command

Let us take a look at the example below:

$ git rebase origin/master

This command implies that we want to rebase a branch from our upstream’s master branch. However, the command by itself cannot complete the job.

You will need to fetch from the upstream master branch before you run this command.

Alternatively, you can use the traditional way:

  1. Switch to your master branch with the git checkout master command.
  2. Pull from your upstream master branch with the git pull origin master command.
  3. Switch back to the branch you want to rebase with the git checkout <branch-name> command.
  4. Now, you can run the git rebase origin/master.

This should update the commits in your branch with the commits in the upstream master branch.

the git rebase origin master Command

The git rebase origin master is a combination of two commits.

$ git checkout master

and

$ git rebase origin

We all know that git rebase origin means that we want to rebase from the tracking branch of origin or, in other words, our upstream. Hence, we can deduce that git rebase origin master will switch to our master branch and rebase it from the remote tracking branch.

The command will fail if you don’t have a remote tracking branch for your master branch. You can remedy this by setting an upstream tracking branch, as shown below:

$ git branch --set-upstream-to=origin/master

Ensure you are checked out in your master branch before running the command above.

Conclusion

From the above, we can conclude that the git rebase origin master is used to rebase the master branch from the upstream tracking branch, while the git rebase origin/master is used to rebase a specific branch from the upstream master branch.

However, the git rebase origin/master will not get the new commits in the upstream master branch. You must update your local master branch before rebasing.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Rebase