devops-notes/09_Git_Branching_Merging.md
2026-01-09 18:14:04 +05:30

1.7 KiB
Raw Permalink Blame History

Git Branching & Merging (Day 9)

Why Branching is Important?

Branching allows teams to:

  • work on features safely
  • fix bugs without breaking main code
  • collaborate without conflicts

DevOps uses branching DAILY.


What is a Branch?

A branch is a separate line of development.

Main branch = stable production code
Feature branch = new changes


View Branches

git branch

Create a New Branch

git branch feature-login

Switch to branch:

git checkout feature-login

Shortcut (create + switch):

git checkout -b feature-login

Check Current Branch

git branch

(* shows active branch)


Make Changes in Branch

echo "Login feature" >> login.txt
git add .
git commit -m "add login feature"

Merge Branch into Main

Switch to main:

git checkout main

Merge:

git merge feature-login

Delete Branch (After Merge)

git branch -d feature-login

Merge Conflicts (Important)

Occurs when same file is changed in two branches.

Git will stop and ask you to fix manually.

Steps:

  1. Open conflicted file
  2. Fix content
  3. Save file
  4. Add & commit
git add .
git commit -m "resolve merge conflict"

Real DevOps Scenario

Problem: Multiple developers pushing code

Solution:

  • Each dev uses own branch
  • Merge after testing

Common Mistakes

  • Working directly on main
  • Not pulling latest code
  • Deleting branch before merge

DevOps Rule

One feature = one branch
Direct commits to main


Summary

Today I learned Git branching, merging, and conflict handling. This is essential for DevOps collaboration.