![]() |
VOOZH | about |
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).
While the master branch serves as the primary branch for stable code, there are scenarios where creating a branch from another branch becomes necessary:
Follow the below given steps for creating a branch from another branch:
Open Command Prompt or PowerShell (or Git Bash if you have Git installed).
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/repositoryBefore 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 -aThe 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 mainWrite the below command for creating the new branch and get switched to that branch.
git checkout -b new-branch-nameTo see all the branches list present , write the below command:
git branch The new git branch features/new-feature has been added successfully.
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:
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.