How to Merge Pull Request in Git: Simple Steps
To merge a pull request, use the
git merge command after fetching the branch or merge it directly on platforms like GitHub by clicking the Merge pull request button. This combines the changes from the feature branch into the target branch, usually main or master.Syntax
The basic syntax to merge a pull request locally is:
git checkout target-branch: Switch to the branch you want to merge into.git merge source-branch: Merge the changes from the source branch (the pull request branch) into the current branch.
On platforms like GitHub, you simply click the Merge pull request button on the pull request page.
bash
git checkout main git merge feature-branch
Example
This example shows how to merge a pull request branch named feature-branch into the main branch locally using Git commands.
bash
git checkout main
git pull origin main
# Make sure main is up to date
git merge feature-branch
# Merge feature-branch into main
git push origin main
# Push the merged changes to remote repositoryOutput
Switched to branch 'main'
Already up to date.
Updating 1a2b3c4..5d6e7f8
Fast-forward
file.txt | 2 ++
1 file changed, 2 insertions(+)
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 350 bytes | 350.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To https://github.com/user/repo.git
1a2b3c4..5d6e7f8 main -> main
Common Pitfalls
Common mistakes when merging pull requests include:
- Trying to merge without switching to the target branch first.
- Not pulling the latest changes from the remote before merging, causing conflicts.
- Ignoring merge conflicts instead of resolving them properly.
- Using
git mergewithout understanding the difference between fast-forward and no-fast-forward merges.
Always ensure your local branches are up to date and resolve conflicts carefully.
bash
# Wrong way: # Trying to merge without switching branch git merge feature-branch # Right way: git checkout main git pull origin main git merge feature-branch
Quick Reference
| Command | Description |
|---|---|
| git checkout main | Switch to the main branch (target branch) |
| git pull origin main | Update local main branch with remote changes |
| git merge feature-branch | Merge feature-branch into main |
| git push origin main | Push merged changes to remote repository |
| Click 'Merge pull request' on GitHub | Merge pull request via GitHub web interface |
Key Takeaways
Always switch to the target branch before merging a pull request branch.
Pull the latest changes from remote to avoid conflicts before merging.
Use the GitHub interface for simple merges if you prefer a graphical method.
Resolve merge conflicts carefully to keep code stable.
Push merged changes to the remote repository to share updates.