Sitemap
Dhanraj Kshirsagar DK

Dhanraj Kshirsagar DK

🚀 AWS & DevOps Engineer | Passionate about cloud, automation & CI/CD | Writing on AWS, Docker, Kubernetes & real-world DevOps practices .

Understanding Branching in Git

4 min readOct 2, 2025

--

Press enter or click to view image in full size

At its core, branching allows you to diverge from the main line of development and continue work in isolation, without touching the “main” branch.

A branch is essentially a pointer to a specific commit in the repository’s history. When you commit on a branch, that pointer advances. Git’s internal model is efficient, so branches are lightweight—creating, switching, and deleting branches is cheap and fast compared to older version control systems.

Why Use Branches?

Here are the main reasons branching is so useful in collaborative development:

Isolation of features/bug fixes: You can build a new feature or fix a bug on its own branch, without affecting the stability of the main or production code.

Parallel development: Multiple developers (or yourself) can work concurrently on independent branches without interfering.

Safer code integration: Changes can be reviewed, tested, and merged only when they’re ready.

Cleaner history: You can keep the main (or master) branch relatively clean, stable, and deploy-ready.

Branching Commands & Workflow Basics

Here are some common commands and a basic workflow:

Create & switch to a branch

git branch feature-xyz # create branch
git checkout feature-xyz # switch to it
# or in recent Git versions:
git switch -c feature-xyz # create + switch

At this point, feature-xyz and main point to the same commit; new work will move the feature-xyz pointer forward.

Work & commit

Make changes, stage them (git add), commit (git commit). These commits live on your feature branch.

Merge back to main

Once the feature is ready:

git checkout main
git merge feature-xyz

This integrates the work into main. If there are conflicts, Git will pause and ask you to resolve them.

After merging, you can delete the branch:

git branch -d feature-xyz

If it’s not yet merged (or Git thinks so), you can force delete with -D.

Working with remote branches

To share your branch with others:

git push origin feature-xyz

And to delete a remote branch:

git push origin --delete feature-xyz

Branching Strategies / Models

Depending on the team size, project demands, release cycles, etc., several branching strategies are common. Here are a few:

Git Flow

Developed by Vincent Driessen, Git Flow uses multiple long-lived branches:

main (or master) for stable, production-ready code

develop for integration of new features

Get Dhanraj Kshirsagar DK’s stories in your inbox

Join Medium for free to get updates from this writer.

Feature branches branch off develop and merge back into develop

release branches for preparing a release (bug fixes, final polishing)

hotfix branches off main to fix urgent production bugs, then merged back into both main and develop

This model is structured and clear, but can feel heavy for fast-moving or small teams.

GitHub Flow (Feature Branching)

A simpler model:

main is always deployable

Create a branch per feature or fix

Use Pull Requests (PRs) for code review

Merge into main when ready, then deploy

Advantages: lightweight, fast, good for continuous deployment scenarios.
Drawback: less structure when many parallel features or longer release cycles.

GitLab Flow

Mixes ideas from both Git Flow and GitHub Flow:

Often has a branch for production (deployable code) and a branch for main (development)

Feature branches merge into main

main is merged to production when ready

May include staging or environment branches depending on need

Advanced Considerations & Tips

Rebasing vs. Merging: Some teams prefer to rebase feature branches onto main before merging, to maintain a linear history. However, rebasing rewrites commit history, so use with care (especially on shared branches).

Branch naming conventions: Use clear, descriptive names like feature/login-page, bugfix/issue-123, hotfix/crash-on-startup.

Delete stale branches: Once merged, clean them up to reduce clutter.

Protect critical branches: You can configure permissions so that main, develop, or production can’t be pushed directly — only via PRs or merges.

Automate checks: Use CI (continuous integration) to run tests, checks, lints on branch merges.

Keep branches short-lived: The longer a branch diverges from main, the harder merging becomes (more conflicts, drift).

Under the Hood: What Really Is a Branch?

Many intuitions about branches are helpful but slightly off. In Git:

A branch is not a “copy” of the project files; it’s a pointer to a commit.

Internally, branches are stored as files in .git/refs/heads/ that each contain the SHA-1 hash of the commit they currently point to.

The “HEAD” pointer tracks which branch you currently have checked out.

Even if a branch seems to “fork” off main, that relationship is implicit by shared history — Git doesn’t inherently consider “parent branches.”
Understanding this helps you reason about merges, rebases, and conflicts more clearly.

Conclusion

Branching is one of Git’s most powerful features. Master it, and you get:

safer, modular development

clearer collaboration

easier experimentation

better history organization

--

--

Dhanraj Kshirsagar DK
Dhanraj Kshirsagar DK

Written by Dhanraj Kshirsagar DK

🚀 AWS & DevOps Engineer | Passionate about cloud, automation & CI/CD | Writing on AWS, Docker, Kubernetes & real-world DevOps practices .