Branching, Merging, and Pull requests
Branching, Merging, and Pull Requests
Branching
A branch is like creating a parallel copy of your project where you can make changes without touching the main version.
Think of it like:
Main branch = your “production-ready” project
New branch = your experimental workspace
Why branches are awesome:
- You can work on a new feature safely.
- You can fix bugs without breaking other things.
- Multiple team members can work in parallel.
Common commands:
Create a branch:
git branch feature-login
Switch to the branch:
git checkout feature-login
Or create + switch in one step:
git checkout -b feature-login
Merging
Once you’re done with your changes on a branch, you bring them back into another branch (usually main) through a merge.
Two types of merges:
Fast-forward merge
Happens when the target branch has no new commits. It simply “moves forward” to include the changes.
Three-way merge
Used when both branches have evolved differently. Git creates a merge commit that unifies history.
Example:
Switch to main:
git checkout main
Get latest changes:
git pull
Merge your feature branch:
git merge feature-login
If there are conflicts, Git will ask you to resolve them manually.
Pull Requests (PRs)
A Pull Request is basically you saying:
“Hey team, I’ve worked on this branch, can someone review it and merge it into main?”
PRs happen on GitHub, not Git itself.
Why PRs matter:
- They force code review (quality control 💯).
- They let teammates discuss changes.
- They show what changed (diff view).
- They enforce workflows via approvals and CI checks.
The flow:
- Push your branch to GitHub:
git push origin feature-login - Go to the repo on GitHub.
- Click “Compare & pull request”.
- Write a description:
- What changed
- Why
- Any risks or dependencies
- Submit the PR.
- Review → Approve → Merge.
Merge Options in GitHub:
- Merge commit — keeps full history.
- Squash and merge — combines all commits into one clean commit.
- Rebase and merge — creates a linear commit history.
Putting It All Together (Real-World Flow)
Your team wants to add a “Contact Form” feature.
- Pull latest code:
git pull origin main - Create a branch:
git checkout -b feature-contact-form - Build your feature.
- Commit your changes:
git add . git commit -m "Added contact form functionality" - Push the branch:
git push origin feature-contact-form - Open a Pull Request on GitHub.
- Team reviews it.
- Merge into main.
- Delete the branch.