Branching & Merging
Branching is how multiple developers work on the same codebase simultaneously. Mastering branches, merges, and rebases is essential for every DevOps workflow.
Branch Fundamentals
# Create a new branch from current location
git branch feature/user-auth
# Switch to that branch
git checkout feature/user-auth
# Or in one step:
git checkout -b feature/user-auth # Create AND switch
git switch -c feature/user-auth # Modern syntax
# List all branches (local + remote)
git branch -a
# Merge feature branch into main
git checkout main
git merge feature/user-auth
# Merge with a merge commit (preserves history)
git merge --no-ff feature/user-auth
# Rebase — replay commits on top of main (cleaner history)
git checkout feature/user-auth
git rebase main
# Delete a branch after merge
git branch -d feature/user-auth # Safe delete
git branch -D feature/user-auth # Force delete
git push origin --delete feature/user-auth # Delete remote branch
# View branch history visually
git log --oneline --graph --all --decorateMerge vs Rebase
- Merge: Combines histories — creates a merge commit. Non-destructive — preserves original history. Best for public branches (main, develop)
- Rebase: Rewrites history — replays commits on top of another branch. Creates a linear, clean history. Best for private feature branches before merging
- Golden rule: Never rebase a public/shared branch — it rewrites history that others depend on
- Squash merge: Combine all feature branch commits into one clean commit on main — used by many companies
Quick Quiz
Tip
Tip
Practice Branching Merging in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Choose based on team size, release cadence, and deployment model
Practice Task
Note
Practice Task — (1) Write a working example of Branching Merging from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Branching Merging is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready devops code.
Key Takeaways
- Branching is how multiple developers work on the same codebase simultaneously.
- Merge: Combines histories — creates a merge commit. Non-destructive — preserves original history. Best for public branches (main, develop)
- Rebase: Rewrites history — replays commits on top of another branch. Creates a linear, clean history. Best for private feature branches before merging
- Golden rule: Never rebase a public/shared branch — it rewrites history that others depend on