How to create a Branch from another Branch in Git

Updated onbyAlan Morel
How to create a Branch from another Branch in Git

Branching is one of the most important features of Git.

The idea of branching is that you can create a new working environment using the state of another working environment.

This allows you to isolate your own and prevent it from interfering with other work.

In this post, we'll learn how to create a new branch from an existing branch in Git.

Getting on the correct Branch

When you want to make a branch from another branch, first you'll want to be on the desired branch.

To check what branch you're currently on, you can use the git status command.

BASH
git status

You'll get output similar to this:

BASH
git status On branch main Your branch is up to date with 'origin/main'.

If you're on the desired branch, you can proceed. If not, you'll have to switch to that branch first.

To switch branches, use the git checkout command, followed by the branch name:

BASH
git checkout <branch-name>

Creating a New Branch

Now that you're on the desired branch, you can create a new branch using the git branch command.

Pass it the name of the new branch as an argument:

BASH
git branch <branch-name>

Now you have a new branch called <branch-name>, that is essentially a copy of the previous branch that you were just on.

Now you can simply switch to that new branch by using the git checkout command, followed by the name of the branch:

BASH
git checkout <branch-name>

At this point, you've successfully created a new branch off of an existing branch.

Conclusion

In this post, we learned how to create a new branch from an existing branch in Git by switching to the desired branch and then creating a new branch off of it.

This is a powerful feature of Git, and it's very useful, especially when working in a larger team or project.

Thanks for reading!

To learn more about web development, founding a start-up, and bootstrapping a SaaS, follow me on X!
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.