Skip to main content

Creating a branch in Git

Introduction

In this guide, you will learn how to create a new branch in Git. Branches are used to isolate work on a specific feature or bug fix, allowing you to work on multiple tasks simultaneously without affecting the main codebase. By following the steps outlined in this guide, you will be able to create a new branch in your local repository and start working on your changes.

Prerequisites

Before you begin, make sure you have the following:

Steps to Create a Branch

Follow these steps to create a new branch in your local repository:

1. Check the Current Branch

Before creating a new branch, it's a good practice to check the current branch you are on. Open a terminal window and navigate to the directory where your local repository is located. Run the following command to see the current branch:

git branch

This command will list all the branches in your local repository and highlight the branch you are currently on.

2. Create a New Branch

To create a new branch, run the following command:

git checkout -b <branch-name>

Replace <branch-name> with the name you want to give to your new branch. For example, if you want to create a branch for a new feature called new-feature, you would run:

git checkout -b new-feature

This command creates a new branch and switches to it, allowing you to start working on your changes.

3. Verify the New Branch

To verify that the new branch has been created successfully, run the following command:

git branch

This command will list all the branches in your local repository, and you should see the new branch you just created highlighted.

4. Start Working on Your Changes

Now that you have created a new branch, you can start working on your changes without affecting the main codebase. Make the necessary modifications to your files and commit your changes following the commit message standards.

5. Push the New Branch to the Remote Repository

If you want to share your changes with others or collaborate on a feature, you can push the new branch to the remote repository. Run the following command to push the new branch to the remote repository:

git push -u origin <branch-name>