Git

How to Create Branch From Another Branch in Git

Git is a decentralized version control system utilized when a team works on the same project. Git is feasible when various people want to track the progress and the configured changes. It permits the users to create multiple repositories. Inside these repositories, you can create multiple branches from other branches to track the new development.

In this blog, we will explain the method of creating a branch from another branch using “git checkout” and “git branch” commands. So, let’s get started!

How to Create Branch From Another Branch Using git checkout Command?

The biggest advantage of Git is the flexibility and power of its branching model, which makes it easy to create and manage branches. If you want to create a branch from another branch in Git for the development purpose or to fix bugs, follow the below-given steps.

Step 1: Open Git Bash
Firstly, open up the Git Bash terminal using the “Startup” menu:

Step 2: Navigate to Git Directory
Move to the specified directory in which you need to create a branch using the “cd” command:

$ cd "C:\Users\nazma\Git\Linuxhint"

Here, “Linuxhint” is our local Git directory which is placed in “Git” folder:

Step 3: Create Branch and Switch to it
Now, execute the “git checkout” command with the “-b” option to create the new branch:

$ git checkout -b develop master

For instance, we have created “develop” branch inside the “master” branch and switch to it:

Step 4: View Branch List
View the list of the local branches in the terminal. Here, the “-a” flag is added to list all present branches:

$ git branch -a

As you see, our newly created “develop” branch has an asterisk “*” sign, which indicates that it is our current working branch:

Step 5: Switch Branch
You can switch to the root or master branch whenever needed:

$ git checkout master

Let’s check out another method to create a new branch.

How to Create Branch From Another Branch Using git branch Command?

Utilize the “git branch” to make a branch from another branch in Git without switching to it directly, Utilize the “git branch” and follow the given procedure.

Step 1: Create Branch
Execute the “git branch” command to create a new branch:

$ git branch without_switching

Below image indicates that we have successfully created a new branch named “without_switching”:

Step 2: Switch to Branch
Switch to the newly created branch using the “git checkout” command:

$ git checkout without_switching

We have effectively elaborated the procedure of creating a branch from another branch in Git.

Conclusion

To create a new branch from an existing branch in Git, navigate to the Git repository in which you need to create a branch. Next, to create a new branch, execute the “$ git checkout -b” command and switch it. You can also create a branch without switching to it directly using the “$ git branch” command. This blog demonstrated the method of creating a branch from the existing branch in Git.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.