DevOps Tutorial · Git and CI/CD · Lesson 12 of 25

Branches, Pull Requests and Merging

Work in branches, review with pull requests, merge safely, and choose between trunk-based development and long-lived branches.

Create a free account to save this page, take notes and track your progress. Sign in

Branches

A branch is a movable pointer to a commit. Create one per change, push it, and open a pull request (merge request) so teammates review it and CI tests it before it reaches main.

A feature branch merged into main
git init -q -b main svc && cd svc
echo "v1" > app.txt && git add . && git commit -qm "Initial"
git switch -qc feature/login
echo "login page" > login.txt && git add . && git commit -qm "Add login page"
git switch -q main
git merge -q --no-ff feature/login -m "Merge feature/login"
git log --oneline --graph

Output

*   3366835 Merge feature/login
|\  
| * 227d84a Add login page
|/  
* f926e34 Initial

Run this on your computer — see “Get Started” for setup.

Merge conflicts

When two branches change the same lines, Git can't decide and stops with a conflict. Edit the file to keep the right result, remove the <<<<<<< / ======= / >>>>>>> markers, then git add and commit.

Branching strategies

Trunk-based development Long-lived branches (GitFlow)
Branches Short-lived (hours to a day or two) off main develop, release/*, hotfix/* living for weeks
Unfinished work Hidden behind feature flags Kept on branches
Merges Small, frequent, few conflicts Big, rare, painful
Fits Continuous delivery, web services Versioned products with several supported releases

DORA research links trunk-based development with higher delivery performance. Protect main with required reviews and passing CI checks.

A merge conflict
git init -q -b main svc && cd svc
echo "timeout = 10" > settings.txt && git add . && git commit -qm "Initial"
git switch -qc ana && echo "timeout = 20" > settings.txt && git commit -qam "Ana: 20"
git switch -q main && echo "timeout = 5" > settings.txt && git commit -qam "Ben: 5"
git merge ana > /dev/null 2>&1 || echo "merge conflict!"
cat settings.txt

Output

merge conflict!
<<<<<<< HEAD
timeout = 5
=======
timeout = 20
>>>>>>> ana

Run this on your computer — see “Get Started” for setup.

Test yourself

What does trunk-based development use to merge unfinished work safely?

  1. Feature flags
  2. Release branches that live for months
  3. Force-pushes
  4. Separate repositories
Show answer

Answer: Feature flags

Small changes merge to main daily; incomplete features stay switched off behind flags.

Was this page helpful?