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 Solid
Managing PHP Dependencies with Composer
Create an RSS Reader in Node
Getting Started with Electron
How to build a Discord bot using TypeScript
How to deploy a Deno app using Docker
Getting Started with Deno
How to deploy a MySQL Server using Docker
How to deploy a Node app using Docker
Using Puppeteer and Jest for End-to-End Testing
Getting User Location using JavaScript's Geolocation API
Setting Up Stylus CSS Preprocessor
