git pull vs git merge:
๐น 1. git pull
- Definition:
- A single command that fetches changes from a remote branch and merges them into your current branch.
-
Equivalent to running:
git fetch git merge origin/<branch-name>
-
Usage:
git pull origin main๐ Gets the latest changes from the remote
mainbranch and merges into your localmain. - Key Point:
- It directly updates your branch with remote changes.
- Faster, but you may not notice merge conflicts until after running it.
๐น 2. git merge
- Definition:
- A command to combine two branches.
- Used locally when you already have both branches.
-
Usage:
git checkout main git merge feature-branch๐ Merges changes from
feature-branchintomain. - Key Point:
- You control when and what branch to merge.
- Doesn’t automatically fetch remote updates.
๐น Comparison Table
| Feature | git pull ๐ข |
git merge ๐ต |
|---|---|---|
| Purpose | Fetch + merge remote changes into current branch | Merge one local branch into another |
| Scope | Works with remote → local | Works with local → local |
| Internally Does | git fetch + git merge |
Only merge |
| Use Case | Keep local branch updated with remote | Combine feature branch into main/dev |
| Example | git pull origin main |
git merge feature-branch |
๐น Example Flow
-
You’re on
mainbranch:git pull origin main๐ Brings remote
mainupdates into your localmain. -
You finished working on
feature-1branch and want to merge intomain:git checkout main git merge feature-1๐ Combines
feature-1changes intomain.
โ In short:
git pull= fetch + merge from remote.git merge= merge one branch into another (local operation).