← Back to Articles         
Git Branch Basics and Branch Management

Git Branch Basics and Branch Management

Published on 03 Oct 2026     4 min read Git & GitHub
Git Branching

Git Branch Basics and Branch Management

1. What is a Git branch, and why do we use branches?

What is a Git Branch?

A Git branch is an independent line of development in a Git repository.

It allows developers to work on new features, bug fixes, or experiments without directly changing the main codebase.

For example:

main
  |
  A ---- B ---- C
              \
               D ---- E
                feature-login

Here:

  • main contains the stable code.
  • feature-login is a separate branch.
  • Changes made in feature-login do not immediately affect main.
  • Once the feature is completed and tested, it can be merged into main.

Why do we use branches?

Branches allow multiple developers to work independently on different changes.

For example, suppose your application is currently stable:

main

You need to develop a login feature. Instead of modifying main directly, create a branch:

main
  |
  +---- feature-login

You work and commit your changes in feature-login:

main
  |
  +---- feature-login
          |
          Commit 1
          |
          Commit 2

After testing, you can merge it:

feature-login
      ↓
     merge
      ↓
main

This protects the stable branch while development is happening.

Common Branch Types

In real projects, you may see branches such as:

main
develop
feature/login
feature/payment
bugfix/login-error
release/2.0
hotfix/payment-issue

For example:

main
 ├── feature/login
 ├── feature/payment
 └── bugfix/login-error

Different teams use different branching strategies, so these names are conventions rather than Git requirements.

Key Points

  • A branch represents a separate line of development.
  • It allows developers to work without directly affecting the main branch.
  • Multiple developers can work on different branches simultaneously.
  • Branches are commonly used for features, bug fixes, releases, and hotfixes.
  • After development is completed, changes can be merged into another branch.
  • Git branches are lightweight because Git primarily tracks them as references to commits.

2. How do you create, switch, and delete a branch?

Git provides commands for managing branches.

View Existing Branches

git branch

Example:

* main
  feature-login
  feature-payment

The * indicates the branch you are currently on.


Create a Branch

git branch feature-login

This creates:

feature-login

But it does not switch to that branch.

You remain on your current branch.

For example:

git branch feature-login
git branch

Output:

* main
  feature-login

Switch to a Branch

Modern Git:

git switch feature-login

Now:

git branch

Output:

  main
* feature-login

Older Git syntax:

git checkout feature-login

git switch is generally clearer when the intention is specifically to switch branches.


Create and Switch in One Command

A common command is:

git switch -c feature-login

This performs both operations:

Create feature-login
        ↓
Switch to feature-login

Older syntax:

git checkout -b feature-login

Example:

git switch -c feature-login

# Make changes

git add .
git commit -m "Add login feature"

The commit is now part of the feature-login branch.


Delete a Branch

First switch away from the branch you want to delete:

git switch main

Then:

git branch -d feature-login

-d performs a safe delete. Git normally prevents deletion when the branch contains changes that have not been merged.

To force deletion:

git branch -D feature-login

Use -D carefully because it can delete an unmerged branch.

Complete Simple Example

# Check branches
git branch

# Create and switch to a new branch
git switch -c feature-login

# Make changes and commit
git add .
git commit -m "Implement login feature"

# Return to main
git switch main

# Merge the feature
git merge feature-login

# Delete the completed branch
git branch -d feature-login

The overall flow is:

main
 │
 ├── Create branch
 │
 ▼
feature-login
 │
 ├── Develop
 ├── Commit
 │
 ▼
Switch to main
 │
 ├── Merge feature-login
 │
 ▼
Delete feature-login

Key Points

  • git branch → lists local branches.
  • git branch <name> → creates a branch but does not switch to it.
  • git switch <name> → switches branches.
  • git switch -c <name> → creates and switches to a branch.
  • git branch -d <name> → safely deletes a merged branch.
  • git branch -D <name> → force-deletes a branch.
  • You normally cannot delete the branch you are currently using.
  • Deleting a local branch does not automatically delete the corresponding remote branch.
← Back to Articles