git

🏠 Homepage 🏠 Syllabus of Day 2 ⬅️ Previous: 👥 Adding Collaborators

8. Merging the PR into Main

What is Merging?

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.

Ways to Merge Changes

There are multiple ways to bring changes from a remote repository into your local branch:

1. Merging via GitHub Pull Request


2. Merging Locally with git pull

git pull origin main

Example:

git checkout feature-branch
git pull origin main
# This brings changes from remote main into your feature-branch

3. Merging Locally with git fetch + git merge

git fetch origin main
git merge origin/main

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

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

Summary Table

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