Git Branch – Creating, Switching, Merging & Managing Branches

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

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 branch

List remote branches

git branch -r

Local + remote

git branch -a

3. Creating Branches

Create a new branch

git branch feature/login

Create and switch in one command

git switch -c feature/login

or older command:

git checkout -b feature/login

4. Switching Between Branches

Switch to existing branch

git switch main

Older syntax:

git checkout main

Warning:

You must have a clean working directory (or stash your changes) before switching.


5. Renaming Branches

Rename current branch

git branch -m new-name

Rename another branch

git branch -m old-name new-name

Update remote branch name

git push origin -u new-name
git push origin --delete old-name

6. Deleting Branches

Delete merged branch

git branch -d feature/login

Force delete (even if unmerged)

⚠️ Dangerous

git branch -D feature/login

Delete remote branch

git push origin --delete feature/login

7. Merging Branches

Merging brings branch changes into another branch.

Example: Merge feature/login into main.

git switch main
git merge feature/login

Fast-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 commit

9. Viewing Branch History / Graphs

Simple graph

git log --graph --oneline --all

More detailed graph

git log --graph --decorate --oneline --all

This 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/dev

When creating a new branch:

git push -u origin feature/login

Now your branch knows where to push & pull from.


11. Rebasing Instead of Merging (Advanced)

Rebase rewrites history:

git switch feature/login
git rebase main

Use 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-redesign

Used heavily in GitHub Flow.


📌 Release Branching

release/1.2
release/2.0

CI/CD pipelines often trigger artifact creation from release branches.


📌 Hotfix Branching

For production issues:

hotfix/payment-bug

Steps:

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

ActionCommand
List branchesgit branch
Create branchgit branch name
Create & switchgit switch -c name
Switchgit switch name
Deletegit branch -d name
Force deletegit branch -D name
Mergegit merge branch
Push remotegit push -u origin branch
Delete remotegit push origin --delete branch
Rebasegit rebase branch
Show graphgit 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.

Leave a Reply

Your email address will not be published. Required fields are marked *