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.
git status
You'll get output similar to this:
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:
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:
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:
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!
If you want to learn about web development, founding a start-up, bootstrapping a SaaS, and more, follow me on X! You can also join the conversation over at our official Discord!
Leave us a message!