Open In App

Create a Git Branch From Another Branch

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Git, branching allows developers to diverge from the main line of development and continue working independently without affecting the main codebase. Creating a new branch from an existing branch is a common scenario when working in Git, especially when you want to develop a new feature or fix a bug without affecting the ongoing work in the main branch (usually master or main).

  • Master Branch: The master branch typically represents the main codebase, stable and production-ready.
  • Feature Branches: Developers create feature branches to work on specific features or fixes. These branches are typically created from the master branch.
  • Merge and Rebase: Once work on a feature branch is complete, changes are merged back into the master branch using either merge or rebase strategies.

Why Create a Branch from Another Branch?

While the master branch serves as the primary branch for stable code, there are scenarios where creating a branch from another branch becomes necessary:

  • Feature Development: You might have a develop branch where all the ongoing features are being worked on. Instead of working directly on develop, you can create a new branch from it for each feature.
  • Bug Fixes: If there is a specific bug in a feature branch, you might want to create a bug-fix branch from it.
  • Experimentation: Sometimes, you want to try something new in the middle of your project without interfering with the primary work. Creating a branch from another branch allows you to do that.

How to Create a Branch From Another Branch

Follow the below given steps for creating a branch from another branch:

Step 1: Open Git Bash

Open Command Prompt or PowerShell (or Git Bash if you have Git installed).

Step 2: Navigate to Git Directory

You must be inside the local Git repository (the folder where your Git project is initialized). If you’re not already inside it, use the cd (change directory) command to navigate to the repository’s folder. For example:

cd /path/to/your/repository

Step 3: Ensure You Are on the Correct Base Branch

Before creating a new branch, ensure you’re on the branch from which you want to create the new branch. For example, if you want to create a branch from main, first check if you are on the main branch by running:

git branch -a

The current branch will be highlighted with an asterisk (*). If you are not on the main branch, switch to it by using the following command:

git checkout main
Screenshot-2025-04-15-154505

Create a Git Branch From Another Branch

Step 4: Create a New Branch

Write the below command for creating the new branch and get switched to that branch.

git checkout -b new-branch-name
Screenshot-2025-04-15-152241

Create a Git Branch From Another Branch

Step 5: View the New Branch

To see all the branches list present , write the below command:

git branch 
Screenshot-2025-04-15-152518

Create a Git Branch From Another Branch

The new git branch features/new-feature has been added successfully.

Best Practices for Branching in Git

Creating branches in Git is a simple yet powerful way to manage development workflows. However, there are a few best practices you should follow when working with branches:

  • Branch Naming Convention: Use clear and consistent names for your branches.
  • Keep Branches Small: Try to keep your branches small. This helps avoid large merge conflicts and makes it easier to manage changes.
  • Sync Your Branch Regularly: Regularly pull from the base branch (like develop or main) to keep your branch up-to-date with the latest changes. This reduces the chances of conflicts when merging.
  • Delete Branches After Merging: Once your work is merged, delete the branch to keep your repository clean and organized.

Conclusion

Creating a branch from another branch in Git is a fundamental operation that allows developers to work on different features, fixes, or experiments independently. By following the steps, you can effectively manage your Git branches, keeping your codebase organized and easy to maintain.


Next Article
Article Tags :

Similar Reads