How to create a Branch from another Branch in Git
Table of Contents
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.
BASHgit status
You'll get output similar to this:
BASHgit 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:
BASHgit 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:
BASHgit 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:
BASHgit 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!
- Getting Started with TypeScript
- Getting Started with Solid
- Getting Started with Express
- Create an RSS Reader in Node
- Getting Started with Electron
- Getting Started with Sass
- Learn how to use v-model with a custom Vue component
- Getting Started with Handlebars.js
- Learn how to build a Slack Bot using Node.js
- Building a Real-Time Note-Taking App with Vue and Firebase
- Getting Started with Vuex: Managing State in Vue
- Getting Started with Moon.js