This guide explains how to safely update a feature branch from master in Git, using either merge or rebase, before creating or completing a pull request. Use this tutorial when your branch is out of date with the master branch, or when modifications are frequently made to the master branch during this time period.
GitHub often shows messages like:
- "This branch is 20 commits ahead of master and 1 commit behind master".
This means:
- Ahead: Number of commits on your feature branch that do not exist on
masteryet (your new feature work). - Behind: Number of commits on
masterthat do not exist on your feature branch (changes from other teams).
Before merging your pull request, you should bring those behind commits from master into your feature branch.
First, ensure your local master branch matches the remote master:
git checkout master
git pull origin masterThis downloads the latest commits from origin/master so that you can use this up‑to‑date master as the base for your feature branch.
This option keeps all history, adding an explicit merge commit. It is simple and safe for shared branches.
From your repository:
git checkout feature # feature is my branch
git pull --ff-only origin feature # optional safety check
git merge masterIf there are conflicts:
- Open the reported files and resolve the conflicts manually.
- Mark them as resolved and create the merge commit:
git add <file1> <file2> ...
git commit # completes the mergePush the updated feature branch:
git push origin featureAfter this, GitHub should show your branch as up to date with master, and your pull request can be merged according to your repository’s merge settings.
This option rewrites the feature branch history so your commits appear "on top" of the latest master, creating a linear history.
From your repository:
git checkout feature
git pull --ff-only origin feature # optional safety check
git rebase masterIf conflicts occur:
# edit files to fix conflicts
git add <file1> <file2> ...
git rebase --continueRepeat until the rebase finishes. If you need to cancel the rebase:
git rebase --abortBecause rebase rewrites commit history, you must force‑push the branch:
git push --force-with-lease origin featureUse --force-with-lease instead of --force to reduce the risk of overwriting others’ work on the same branch.