DevOps · Infrastructure as Code (IaC) · Git Series – Part 11
Branching is one of Git’s most powerful features.
It allows multiple streams of work to happen in parallel without affecting the main codebase.
In DevOps, branching is essential for:
✔ Feature development
✔ Bug fixes
✔ Hotfixes
✔ Infrastructure changes
✔ Release workflows
✔ CI/CD pipelines
This chapter covers:
- What branches are
- How branching works internally
- Creating, switching, renaming branches
- Merging strategies
- Cleaning up old branches
- Viewing branch graphs
- Real DevOps branching workflows
Table of Contents
1. What Is a Git Branch?
A branch is simply a pointer to a commit.
Think of branches as independent timelines of your project.
Default branch names:
main(modern Git & GitHub)master(older repos)
A branch lets you create:
- A new feature
- A bug fix
- A hotfix
- A release
- A testing environment
…without touching stable code.
2. Viewing Branches
List local branches
git branchList remote branches
git branch -rLocal + remote
git branch -a3. Creating Branches
Create a new branch
git branch feature/loginCreate and switch in one command
git switch -c feature/loginor older command:
git checkout -b feature/login4. Switching Between Branches
Switch to existing branch
git switch mainOlder syntax:
git checkout mainWarning:
You must have a clean working directory (or stash your changes) before switching.
5. Renaming Branches
Rename current branch
git branch -m new-nameRename another branch
git branch -m old-name new-nameUpdate remote branch name
git push origin -u new-name
git push origin --delete old-name6. Deleting Branches
Delete merged branch
git branch -d feature/loginForce delete (even if unmerged)
⚠️ Dangerous
git branch -D feature/loginDelete remote branch
git push origin --delete feature/login7. Merging Branches
Merging brings branch changes into another branch.
Example: Merge feature/login into main.
git switch main
git merge feature/loginFast-forward merge
Occurs when main has no new commits since the branch was created.
main ------>
\
feature/login ------>
Git simply moves the branch pointer.
Recursive (3-way) merge
When both branches diverge:
A---B (feature)
/
main-O---C---D
Git creates a new merge commit.
8. Merge Conflicts & How to Resolve Them
Conflicts occur when two branches modify the same line.
You’ll see:
<<<<<<< HEAD
your changes
=======
their changes
>>>>>>> feature/login
Resolve conflicts manually → then:
git add .
git commit9. Viewing Branch History / Graphs
Simple graph
git log --graph --oneline --allMore detailed graph
git log --graph --decorate --oneline --allThis is crucial for understanding merges, diverging branches, and release flows.
10. Upstream Tracking (Very Important)
Set branch to track remote equivalent
git branch -u origin/devWhen creating a new branch:
git push -u origin feature/loginNow your branch knows where to push & pull from.
11. Rebasing Instead of Merging (Advanced)
Rebase rewrites history:
git switch feature/login
git rebase mainUse rebase when:
- You want a clean history
- You prefer linear commits
- You work alone or on feature branches
Avoid rebasing shared branches.
12. DevOps & IaC Branching Use Cases
📌 Feature Branching
Each feature gets a branch:
feature/add-logging
feature/api-auth
feature/ui-redesignUsed heavily in GitHub Flow.
📌 Release Branching
release/1.2
release/2.0CI/CD pipelines often trigger artifact creation from release branches.
📌 Hotfix Branching
For production issues:
hotfix/payment-bugSteps:
git switch main
git switch -c hotfix/payment-bug
# fix bug
git merge hotfix/payment-bug
git tag -a v1.0.1
git push --tags📌 GitOps + Infrastructure-as-Code
Branches may represent environments:
dev
stage
prod
Tools like ArgoCD, FluxCD deploy based on branch state.
13. Summary Table – Branch Commands
| Action | Command |
|---|---|
| List branches | git branch |
| Create branch | git branch name |
| Create & switch | git switch -c name |
| Switch | git switch name |
| Delete | git branch -d name |
| Force delete | git branch -D name |
| Merge | git merge branch |
| Push remote | git push -u origin branch |
| Delete remote | git push origin --delete branch |
| Rebase | git rebase branch |
| Show graph | git log --graph --oneline --all |
14. What’s Next?
You now understand:
✔ Branch creation & switching
✔ Merging & fast-forward
✔ Branch deletion & cleanup
✔ Rebasing basics
✔ DevOps / IaC branch strategies
Next chapter:
➡️ Part 12 – Git Merge: Fast-Forward, 3-Way Merge, Conflicts & Best Practices
We will go deeper into merge types, conflict resolution, merge tools, and DevOps-friendly merging standards.