How to Rebase Local Branch Onto Remote Master
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, replaceorigin
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 themaster
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
Fetch Latest Changes from Remote:
git fetch origin
Switch to Your Local Branch:
git checkout feature-branch
Rebase Onto Remote Master:
git rebase origin/master
Resolve Conflicts (if any):
- Resolve conflicts in your editor.
Stage resolved files:
git add <resolved-file>
- Continue rebase:
```
git rebase --continue
```
Force Push (if needed):
git push --force origin feature-branch
Summary
To rebase your local branch onto the remote master
branch:
- Fetch the latest changes with
git fetch origin
. - Check out your local branch with
git checkout your-branch
. - Rebase onto the remote master with
git rebase origin/master
. - Resolve conflicts, if any, and continue the rebase.
- 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.
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 usBuild 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.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github