🏠 Homepage 🏠 Syllabus of Day 2 ⬅️ Previous: 👥 Adding Collaborators
Merging is the process of integrating changes from one branch (often a feature branch) into another branch (usually main). This is typically done after a pull request (PR) is reviewed and approved.
There are multiple ways to bring changes from a remote repository into your local branch:
git pullgit pull origin main
main, this updates your local main with the remote main.feature-branch), this merges origin/main into your current branch (feature-branch).git pull origin other-branch
This will merge origin/other-branch into your current branch.
Example:
git checkout feature-branch
git pull origin main
# This brings changes from remote main into your feature-branch
git fetch + git mergegit fetch origin main
git merge origin/main
git fetch downloads changes from the remote but does not merge them.git merge origin/main merges the fetched changes into your current branch.main, this updates your local main with the remote main.origin/main into your current branch.git fetch origin other-branch
git merge origin/other-branch
Example:
git checkout feature-branch
git fetch origin main
git merge origin/main
# This brings changes from remote main into your feature-branch, but lets you inspect first
git pull vs git fetch + git merge| Use Case | Recommended Command(s) |
|---|---|
| Personal quick updates | git pull |
| Team collaboration & safety | git fetch + git merge |
| Need to inspect before merging | git fetch + git diff |
git pull: Fast, automatic, but can cause unexpected conflicts.git fetch + git merge: Safer, lets you review changes before merging.Pro Tip:
If you’re unsure or working on shared branches, always prefer:
git fetch
git diff origin/main # optional: inspect changes
git merge origin/main
| Current Branch | Command | Result |
|---|---|---|
| main | git pull origin main |
Updates local main with remote main |
| feature-branch | git pull origin main |
Merges remote main into feature-branch |
| any-branch | git pull origin branch |
Merges specified remote branch into current branch |
| main | git fetch origin main + git merge origin/main |
Updates local main with remote main (with review) |
| feature-branch | git fetch origin main + git merge origin/main |
Merges remote main into feature-branch (with review) |
Tip 1: Always check which branch you are on with git status before pulling or merging, especially in team settings.
Tip 2: Always pull or fetch the latest changes after a merge to keep your local repository up to date.
➡️Up Next: ⚡Resolving Merge Conflicts