The Wayback Machine - https://web.archive.org/web/20261129080049/https://www.geeksforgeeks.org/git/introduction-to-git-branch/

Introduction to Git Branch

Last Updated : 26 Nov, 2026

A Git branch is an independent line of development that allows developers to work on features, bug fixes, and experiments without directly changing another branch. Once the work is complete, changes can be integrated into another branch using merging or rebasing.

  • Allows developers to work on different tasks independently.
  • Helps test new features without affecting other development work.
  • Supports collaboration by allowing multiple developers to work on separate branches.
  • Maintains a separate development history for different tasks.

Working

Git branches allow developers to manage different tasks independently while sharing the same repository and commit history. The workflow generally involves the following steps:

  • The main branch usually contains the primary version of the project.
  • Developers create feature branches to work on new functionality or fix bugs.
  • Changes are saved as commits within the respective branches.
  • Completed changes are reviewed and merged into the target branch.
  • Release and hotfix branches can be used to manage software releases and urgent fixes.
Introduction-to-Git-Branch

Types of Branches

Git does not enforce predefined branch types. Teams use different branch naming conventions and workflows to organize development activities. Some commonly used branch types include:

  • Main or Master Branch: Represents the primary development line and generally contains stable project code.
  • Feature Branch: Created to develop a specific feature without directly modifying the main development branch.
  • Develop Branch: Used in the Gitflow workflow to integrate completed features before preparing a release.
  • Release Branch: Created to prepare a software release, allowing final testing and minor fixes before deployment.
  • Hotfix Branch: Created to address urgent issues in the production version of an application.

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

Displays all local branches in the repository. It can also create a new branch when a branch name is provided.

git branch

To create a new branch:

git branch feature-login

The new branch is created, but the current branch remains unchanged.

Screenshot-2025-03-03-120437
git branch
Screenshot-2025-03-03-120608
git branch

2. git checkout and git switch

Both commands can be used to switch between existing branches.

git checkout pranjal
Screenshot-2025-03-03-120740
git checkout

A modern alternative is:

git switch pranjal

The git switch command is specifically designed for switching branches, while git checkout supports additional operations.

Screenshot-2025-03-03-121353
git switch

3. git checkout -b and git switch -c

These commands create a new branch and immediately switch to it.

git checkout -b vipin
Screenshot-2025-03-03-121129

Alternatively:

git switch -c gfg

Both commands create the specified branch and make it the current branch.

Screenshot-2025-03-03-121553
git switch -c

4. git merge

Combines changes from another branch into the current branch. It is commonly used to integrate completed feature work into the main or develop branch.

git switch main
git merge feature-login

This command merges the changes from feature-login into main.

If Git cannot automatically combine the changes, it reports a merge conflict that must be resolved before completing the merge.

5. git rebase

Reapplies commits from the current branch on top of another branch. It helps maintain a linear commit history.

git switch feature-logingit rebase main

This command reapplies the commits from feature-login on top of the latest commit in main.

Before rebase:

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

After rebase:

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

The commits are recreated with new commit IDs. Avoid rebasing shared commits that other developers may already be using.

git-rebase

6. git branch -d

Deletes a local branch if Git determines that its changes have been merged into its upstream branch or current branch.

git branch -d  vipin

This helps remove branches that are no longer needed after completing development work.

Screenshot-2025-03-03-122554
git branch -d

7. git branch -D

Forcefully deletes a local branch, even if it contains unmerged changes.

git branch -D gfg

Use this command carefully because it can remove a branch containing work that has not been integrated elsewhere.

Screenshot-2025-03-03-123342

8. git branch -a

Displays all local and remote-tracking branches available in the repository.

git branch -a

This helps developers view branches associated with remote repositories along with their local branches.

git-branch--a

9. git branch -m

Renames the current branch or a specified local branch.

git branch -m feature-login feature-authentication

This renames the feature-login branch to feature-authentication.

git-branch

Difference Between Git Merge and Rebase

Git provides merge and rebase operations to integrate changes from one branch into another. Both operations combine development work, but they handle commit history differently.

Git MergeGit Rebase
Combines changes from another branch into the current branch.Reapplies commits on top of another branch.
Preserves the original branch commit history.Creates new commits with different commit IDs.
May create a merge commit.Usually produces a linear commit history.
Useful for preserving the history of collaborative work.Useful for maintaining a cleaner, linear history.

Both operations help integrate changes between branches. The choice depends on the team's collaboration practices and preferred commit history.

Comment

Explore