How to Rebase and Merge Pull Request in Git
To rebase and merge a pull request, first fetch the latest changes and checkout your feature branch, then run
git rebase origin/main to apply your changes on top of the main branch. After resolving any conflicts, push the rebased branch and merge the pull request using git merge or the platform's merge button.Syntax
The basic commands to rebase and merge a pull request are:
git checkout feature-branch: Switch to your feature branch.git fetch origin: Update your local copy of the remote branches.git rebase origin/main: Reapply your commits on top of the latest main branch.git push --force-with-lease: Update the remote branch with the rebased commits safely.git checkout main: Switch to the main branch.git merge feature-branch: Merge the feature branch into main.git push origin main: Push the updated main branch to the remote repository.
This sequence ensures your changes are applied cleanly on top of the latest main branch before merging.
bash
git checkout feature-branch git fetch origin git rebase origin/main git push --force-with-lease git checkout main git merge feature-branch git push origin main
Example
This example shows rebasing a feature branch named feature-login onto the latest main branch, then merging it.
bash
git checkout feature-login git fetch origin # Rebase feature-login on top of origin/main git rebase origin/main # If conflicts occur, resolve them, then continue # git add <resolved-files> # git rebase --continue # Push rebased branch git push --force-with-lease # Switch to main branch git checkout main # Merge feature-login into main git merge feature-login # Push main branch git push origin main
Output
First, git will replay commits from feature-login on top of origin/main.
If conflicts happen, git will pause and ask to resolve them.
After resolving, rebasing continues.
Finally, the rebased branch is pushed with force to update the remote.
Merging feature-login into main creates a fast-forward or merge commit.
Pushing main updates the remote repository.
Common Pitfalls
- Forcing push without
--force-with-lease: This can overwrite others' work if the remote branch changed. - Not resolving conflicts properly: Rebasing stops on conflicts; you must fix and continue.
- Rebasing public branches: Avoid rebasing branches others use to prevent confusion.
- Skipping fetch: Rebasing without fetching latest main can cause outdated merges.
bash
## Wrong: force push without lease # git push --force ## Right: safer force push git push --force-with-lease
Quick Reference
Remember these tips when rebasing and merging pull requests:
- Always fetch latest changes before rebasing.
- Use
--force-with-leaseto safely update remote branches after rebase. - Resolve all conflicts before continuing rebase.
- Merge rebased branch into main to keep history clean.
- Communicate with your team when rewriting history.
Key Takeaways
Always rebase your feature branch onto the latest main branch before merging.
Use 'git push --force-with-lease' to safely update the remote branch after rebasing.
Resolve all conflicts during rebase before continuing.
Merge the rebased branch into main to keep a clean commit history.
Avoid rebasing branches shared with others to prevent confusion.