Introduction to Git Branch (original) (raw)

Last Updated : 15 Jan, 2026

A Git branch is a separate workspace used to make changes without affecting the main project. Once the work is complete, the changes can be merged back into the main or master branch.

Branches make it easy to:

Working of Git Branch

Git branches allow you to manage different tasks or features in isolation without affecting the main project. Here’s how it works:

Types of Branches in Git

Git uses different types of branches to keep things organized and allow you to work on different tasks without disturbing the main project. Here are the most common types of branches you will work with:

Basic Git Branching Commands

Below are some essential Git branching commands that everyone should learn when starting to work with Git branching:

1. git branch

Lists all local branches or creates a new branch when used with a name means it displays a list of all local branches or creates a new branch if a name is specified.

Screenshot-2025-03-03-120437

git branch

2. git branch

Creates a new branch with the specified name without switching to it, allowing you to stay on your current branch.

Screenshot-2025-03-03-120608

git branch

3. git checkout

Switches to an existing branch in the repository, allowing you to continue working from where it was last updated.

Screenshot-2025-03-03-120740

git checkout

4. git checkout -b

Creates a new branch with the specified name and immediately switches to it, allowing you to start working on it right away.

Screenshot-2025-03-03-121129

git checkout -b

5. git switch

A modern alternative to the checkout command, primarily used for switching branches more efficiently and intuitively.

Screenshot-2025-03-03-121353

git switch

6. git switch -c

Creates a new branch with the specified name and switches to it immediately, allowing you to start making changes right away.

Screenshot-2025-03-03-121553

git switch -c

7. git rebase

git rebase main rewrites the feature branch commits so they appear as if they were created from the latest commit of main.

**Before rebase

main: A --- B --- C
\
feature: D --- E

**After rebase

main: A --- B --- C
\
feature: D' --- E'

Screenshot-2025-03-03-122106

git rebase

8. git branch -d

Deletes the specified branch, but only if it has been fully merged into its upstream branch, ensuring no unmerged changes are lost.

Screenshot-2025-03-03-122554

git branch -d

9. git branch -D

Forcefully deletes the specified branch, even if it contains unmerged changes or has not been fully updated, potentially leading to data loss.

Screenshot-2025-03-03-123342

git branch -D