Class 14 of 50 • Branching & Merging

Git Branching: Fundamentals, Creation, Listing, Renaming & Deletion

Branching is Git's superpower. In legacy version control systems like SVN, creating a branch was expensive and slow because it involved duplicating directory trees on a server. In Git, branching is instantaneous and virtually costless. Let's learn why.

Git Branching and Merging Workflow
Figure 14.1: Feature Branch diverging from Common Ancestor on main and converging via Merge Commit.

What is a Git Branch? (The Internal Reality)

In Git, a branch is not an isolated container of files. A branch is simply a lightweight, movable pointer to a specific commit object. That's it! It is literally a 41-byte plain text file inside .git/refs/heads/ containing the 40-character SHA-1 hash of the commit it points to.

Because creating a branch writes only 41 bytes to disk, creating, switching, and deleting branches in Git takes less than 1 millisecond regardless of whether your project contains 10 files or 100,000 files!

Essential Branching Commands

1. Listing Existing Branches

# List local branches (* indicates the currently active branch):
$ git branch
* main
  feature/auth
  bugfix/login-styles

# List both local AND remote-tracking branches:
$ git branch -a

# List branches with their latest commit SHA and title:
$ git branch -v

2. Creating a New Branch

# Create a branch without switching to it:
$ git branch feature/user-profile

# Create a branch from a specific commit hash or tag:
$ git branch hotfix-v1.0 4b8e2a1

3. Renaming Branches

# Rename the currently active branch:
$ git branch -m modern-main

# Rename an arbitrary branch:
$ git branch -m old-name new-name

4. Deleting Branches Safely and Forcefully

# Safe deletion (-d): Deletes branch ONLY if it has been fully merged into upstream:
$ git branch -d feature/auth

# Force deletion (-D): Deletes branch even if it contains unmerged commits:
$ git branch -D experimental-draft

BCA University Viva Question

Question: "What is the difference between git branch -d and git branch -D?"
Answer: "-d (lowercase) is a safety-checked deletion; Git prevents deleting the branch if it has unmerged work that would be lost. -D (uppercase) is a forced deletion equivalent to --delete --force, which discards unmerged commits without warning."

Explore Related Git & GitHub Tutorials & Next Steps

Strengthen your version control mastery by studying these connected classes:

git switch vs git checkout

Modern Git 2.23+ dedicated command for branch switching without side-effects.

Merging Strategies Explained

Integrate completed feature branches into main via fast-forward and 3-way merges.

Resolving Merge Conflicts

Handle overlapping code changes cleanly when two branches edit identical files.

Professional Branching Workflows

Explore GitFlow, GitHub Flow, and Trunk-Based branch management models.

Frequently Asked Questions (FAQ) - Student & Interview Review

There is no practical limit. Repositories can easily support hundreds or thousands of branches simultaneously with zero performance degradation.
Run `git push origin --delete branch-name` (or the older shorthand `git push origin :branch-name`).
Industry teams use prefixes like `feature/`, `bugfix/`, `hotfix/`, `docs/`, and `refactor/` (e.g. `feature/jwt-auth`). This categorizes branches cleanly in GUI tools and allows automated CI/CD filtering.

Next Step in Your Git & GitHub Learning Path:

Advance your software engineering skills with the next topic in our synchronized curriculum.

Continue to git switch & checkout → Previous: ← File Management (mv & rm) | Full Syllabus

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!