Git Branching and Merging: A Practical Guide for DevOps


Welcome, fellas! Today, we’re diving deeper into Git, so stay focused because branching and merging are game-changers! These essential skills allow DevOps engineers to work on multiple features, bug fixes, or experiments in isolation before seamlessly integrating them into the main codebase.
In this blog, we’ll break down the concepts and commands of branching and merging with clear examples and real DevOps use cases—making sure you get hands-on experience along the way.
So buckle up, and let’s get started! I promise this one’s gonna be fun.
Understanding Git Branching
What is a Branch?
A branch in Git is a separate line of development that allows you to work on different tasks independently without affecting the main codebase (typically the main
or master
branch).
Why Use Branches in DevOps?
Parallel Development: Multiple team members can work on different features or fixes concurrently.
Safe Experimentation: New features or experimental changes can be developed in isolation without risking the stability of the production code.
Streamlined Integration: Branches allow you to merge changes back into the main codebase only when they are ready and tested.
Guide to Branching, Making Changes, and Committing to a Branch:
List Existing Branches
Check which branches exist:git branch
Example Use Case:
In a DevOps project, you may see only themain
branch initially, indicating a single production line.DevOps Use Case 1:
Feature Branch for Enhanced Logging: When improving log monitoring in a CI/CD pipeline, create afeature-logging
branch to add and test logging enhancements without affecting the stable code.Create and switch to a feature branch:
git checkout -b feature-logging
Modify
deploy.sh
:echo "echo 'Deployment started at $(date)'" >> deploy.sh
Commit the change:
git add deploy.sh git commit -m "Add timestamp logging to deploy.sh"
Check Status
Verify which files have changed:git status
DevOps Use Case 2:
Hotfix Branch: If a critical bug is discovered in the deployment scripts, create and switch to ahotfix-deploy
branch to fix the issue promptly.Create a hotfix branch from
main
:git checkout main git checkout -b hotfix-deploy
Fix the script:
sed -i 's/apply/apply --wait/g' deploy.sh # Example fix
Commit the hotfix:
git add deploy.sh git commit -m "Fix deployment timeout issue"
Check Status
Verify which files have changed:git status
Check Branches
To check all branches in a Git:git branch -a
The active branch will be highlighted with an asterisk (
*
).
Key Commands
Command | Purpose |
git branch <name> | Create a new branch. |
git checkout <name> | Switch to a branch. |
git checkout -b <name> | Create and switch to a new branch. |
git branch -a | Check all branches. |
Merging Branches
Once your work is complete and tested in your feature or hotfix branch, the next step is to merge these changes back into the main branch.
Goal: Combine changes from a branch (e.g., feature-logging
) into main
.
Step-by-Step: Merging a Branch
Switch Back to Main Branch
Before merging, switch to the branch you want to merge into (typicallymain
):git checkout main
Merge the Feature Branch
Merge your feature branch into the main branch:git merge feature-logging
Feature Integration: Merging the
feature-logging
branch intomain
ensures that the improved logging functionality is now part of your production code.Resolve Merge Conflicts (if any)
If conflicts occur, Git will indicate which files are in conflict. Open these files, resolve the conflicts manually, then:git add <conflicted-file> git commit -m "Resolved merge conflicts between main and feature-logging"
Check the Status
Verify that the merge was successful:git status
You should see a message indicating that your working tree is clean.
Handling Merge Conflicts
Merge conflicts occur when two branches modify the same line of a file. Let’s simulate a conflict.
Step 1: Modify deploy.sh
in main
echo 'echo "Deployment started!"' >> deploy.sh
git add deploy.sh
git commit -m "Modified deployment message in main"
Step 2: Switch to hotfix-deploy
and Modify the Same File
git checkout hotfix-deploy
echo 'echo "Starting deployment process!"' >> deploy.sh
git add deploy.sh
git commit -m "Changed deployment message in hotfix-deploy"
Step 3: Try Merging Again
git checkout main
git merge hotfix-deploy
✅ Output:
CONFLICT (content): Merge conflict in deploy.sh
Step 4: Resolving the Conflict
Open
deploy.sh
in a text editor.Manually choose the correct version (look for
<<<<<<<
,=======
,>>>>>>>
markers).Stage the resolved file and commit:
git add deploy.sh git commit -m "Resolved merge conflict in deploy.sh"
Now, the merge is successful!
DevOps Best Practices
Test Branches First: Validate changes in a branch before merging to
main
.Use Pull Requests (PRs): In platforms like GitHub, PRs enable code reviews before merging.
Keep Branches Short-Lived: Avoid long-running branches to reduce conflicts.
Summary
Workflow | Commands | DevOps Use Case |
Modify & Commit | git add , git commit | Track script/config changes. |
Branching | git checkout -b , git branch | Isolate features/hotfixes. |
Merging | git merge | Integrate tested changes into main . |
DevOps Use Case Examples
Use Case Example 1: CI/CD Pipeline Updates
Scenario: Your team needs to add a new stage in your CI/CD pipeline for code quality checks.
Process:
Create a branch (
feature-code-quality
).Modify pipeline configuration files and add scripts for code quality checks.
Commit your changes and merge the branch into
main
once the tests pass.
Benefit: Allows testing of new pipeline stages without disrupting the stable main pipeline.
Use Case Example 2: Infrastructure Hotfix
Scenario: A bug is identified in your Terraform scripts, that provision cloud resources.
Process:
Create a hotfix branch (
hotfix-terraform
).Correct the script and commit the fix.
Merge the branch back into
main
promptly after verifying the fix.
Benefit: Quick isolation and resolution of critical issues without affecting ongoing development.
Final Thoughts
Branching and merging in Git provide a robust framework for managing changes in a DevOps environment. They allow for parallel development, safe experimentation, and smooth integration of new features or bug fixes.
Phew! That was quite a ride, wasn’t it? I hope it wasn’t too overwhelming! Understanding how actual code is maintained through branching and merging is a game-changer for every DevOps engineer. It’s the backbone of smooth collaboration and efficient version control.
But guess what? We’re not done yet! Up next, we’re leveling up with ‘Advanced Git Workflows for DevOps: Rebasing and Stashing’—where we’ll explore even more powerful ways to keep our Git history clean and our workflow smooth.
Until next time, keep coding, automating, and advancing in DevOps! 😁
Peace out ✌️
Subscribe to my newsletter
Read articles from Rajratan Gaikwad directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Rajratan Gaikwad
Rajratan Gaikwad
I write about the art and adventure of DevOps, making complex topics in CI/CD, Cloud Automation, Infrastructure as Code, and Monitoring approachable and fun. Join me on my DevOps Voyage, where each post unpacks real-world challenges, explores best practices, and dives deep into the world of modern DevOps—one journey at a time!