Create a New Branch

GitGitBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In Git, a branch is a lightweight movable pointer to a commit. Creating a new branch allows you to work on a new feature or bug fix without affecting the main codebase. In this lab, you will learn how to create a new branch in Git.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BranchManagementGroup(["`Branch Management`"]) git/BranchManagementGroup -.-> git/branch("`Handle Branches`") subgraph Lab Skills git/branch -.-> lab-12714{{"`Create a New Branch`"}} end

Create a New Branch

For this lab, fork the Git repository named https://github.com/labex-labs/git-playground into your GitHub account.You are working on a project in a Git repository named https://github.com/your-username/git-playground. You need to create a new branch named feature-1 to work on a new feature.

  1. Clone the repository, navigate to the directory and configure the identity:
git clone https://github.com/your-username/git-playground.git
cd git-playground
git config --global user.name "your-username"
git config --global user.email "your-email"
  1. Check the current branch:
git branch
  1. Create a new branch named feature-1:
git checkout -b feature-1
  1. Verify that you are now on the feature-1 branch:
git branch
  1. Push the changes to the remote repository:
git push -u origin feature-1

This is what happens when you run the git branch -r command:

Summary

Creating a new branch in Git is a simple process that allows you to work on a new feature or bug fix without affecting the main codebase. Use the git checkout -b <branch> command to create a new branch with the specified name and switch to it. You can optionally add -t <remote>/<branch> to set up a remote tracking branch for the newly created branch.

Other Git Tutorials you may like