How to Rebase Local Branch Onto Remote Master

Better Stack Team
Updated on August 12, 2024

Rebasing your local branch onto the remote master branch can help you incorporate the latest changes from the master branch into your branch while keeping a clean commit history. Here’s a step-by-step guide on how to do it:

Steps to Rebase Your Local Branch onto Remote Master

1. Fetch the Latest Changes

First, ensure you have the latest changes from the remote repository. This updates your local references to the remote branches.

 
git fetch origin
  • origin is the default name for your remote repository. If you are using a different remote name, replace origin with that name.

2. Check Out Your Local Branch

Switch to the local branch you want to rebase.

 
git checkout your-branch

Replace your-branch with the name of your local branch.

3. Rebase onto Remote Master

Rebase your local branch onto the updated master branch from the remote repository.

 
git rebase origin/master
  • origin/master refers to the master branch on the remote repository.

4. Resolve Conflicts (if any)

During the rebase, conflicts may arise if there are changes in your branch that conflict with the changes in origin/master.

  • Git will pause and indicate which files have conflicts. Open the conflicting files and resolve the conflicts manually.
  • After resolving conflicts, mark them as resolved:

     
    git add <file>
    
  • Continue the rebase process:

     
    git rebase --continue
    
  • If you want to abort the rebase and return to the state before the rebase, use:

     
    git rebase --abort
    

5. Force Push (if the Branch is Already Pushed)

If your local branch was already pushed to the remote before the rebase, you will need to force push to update the remote branch with the rebased commits.

 
git push --force origin your-branch
  • Warning: Force pushing will overwrite the remote branch and can affect others who are working with the same branch. Ensure that it is safe to force push, or communicate with your team if necessary.

Example Workflow

  1. Fetch Latest Changes from Remote:

     
    git fetch origin
    
  2. Switch to Your Local Branch:

     
    git checkout feature-branch
    
  3. Rebase Onto Remote Master:

     
    git rebase origin/master
    
  4. Resolve Conflicts (if any):

    • Resolve conflicts in your editor.
    • Stage resolved files:

       
      git add <resolved-file>
      
 
- Continue rebase:

    ```
    git rebase --continue
    ```
  1. Force Push (if needed):

     
    git push --force origin feature-branch
    

Summary

To rebase your local branch onto the remote master branch:

  1. Fetch the latest changes with git fetch origin.
  2. Check out your local branch with git checkout your-branch.
  3. Rebase onto the remote master with git rebase origin/master.
  4. Resolve conflicts, if any, and continue the rebase.
  5. Force push your branch to the remote if it was previously pushed.

This process integrates the latest changes from master into your branch while maintaining a linear commit history.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github