How to create new local branch and switch between branches in Git

Learn how to create new local branch and switch between branches in git with practical examples, diagrams, and best practices. Covers git, gitlab, version-control development techniques with visual...

Mastering Git Branches: Create, Switch, and Manage Your Workflow

Hero image for How to create new local branch and switch between branches in Git

Learn the essential Git commands to create new local branches, switch between them, and understand how branches facilitate parallel development in your projects.

Git branches are fundamental to modern software development, enabling developers to work on different features or bug fixes concurrently without affecting the main codebase. This article will guide you through the process of creating new local branches and efficiently switching between them, a core skill for any Git user.

Understanding Git Branches

In Git, a branch is essentially a lightweight movable pointer to one of your commits. When you create a new branch, you're creating a new pointer. The master (or main) branch is the default branch in a Git repository. All your development work typically starts from this branch, and new features or fixes are developed on separate branches before being merged back into the main line of development.

flowchart TD
    A[Start on main/master] --> B{Create new branch 'feature-x'}
    B --> C[Work on feature-x]
    C --> D{Switch back to main/master}
    D --> E[Work on main/master]
    E --> F{Switch to another branch 'bugfix-y'}
    F --> G[Work on bugfix-y]
    G --> H{Merge branches}
    H --> I[End]

Basic Git Branching Workflow

Creating a New Local Branch

To start working on a new feature or bug fix, it's best practice to create a new branch. This isolates your changes from the main codebase until they are ready. The git branch command is used for creating, listing, and deleting branches.

git branch <branch-name>

Command to create a new branch

For example, to create a branch named feature/add-user-profile:

git branch feature/add-user-profile

Creating a new branch named 'feature/add-user-profile'

Switching Between Branches

After creating a branch, you need to switch to it to start making changes. The git checkout command is traditionally used for this purpose. However, Git 2.23 introduced git switch as a more intuitive and safer alternative specifically for switching branches.

Using git checkout:

git checkout <branch-name>

Command to switch to an existing branch using 'checkout'

For instance, to switch to the feature/add-user-profile branch:

git checkout feature/add-user-profile

Switching to 'feature/add-user-profile' using 'checkout'

Using git switch (recommended for newer Git versions):

git switch <branch-name>

Command to switch to an existing branch using 'switch'

To switch to feature/add-user-profile with git switch:

git switch feature/add-user-profile

Switching to 'feature/add-user-profile' using 'switch'

Creating and Switching in One Command

Git provides a convenient shortcut to create a new branch and immediately switch to it. This is often the most common workflow when starting a new task.

Using git checkout -b:

git checkout -b <new-branch-name>

Command to create and switch to a new branch using 'checkout'

Example:

git checkout -b bugfix/login-issue

Creating and switching to 'bugfix/login-issue'

Using git switch -c (recommended for newer Git versions):

git switch -c <new-branch-name>

Command to create and switch to a new branch using 'switch'

Example:

git switch -c bugfix/login-issue

Creating and switching to 'bugfix/login-issue' using 'switch'

Listing Branches

To see all local branches in your repository and identify which one you are currently on, use the git branch command without any arguments.

git branch

Listing all local branches

The output will show a list of branches, with an asterisk * indicating the currently active branch.

  bugfix/login-issue
* feature/add-user-profile
  main

Example output of 'git branch'

This output indicates that you are currently on the feature/add-user-profile branch.