AlgoMaster Logo

Creating Branches

Last Updated: January 3, 2026

6 min read

When you create a branch in Git, you are essentially creating a new pointer to a specific commit. This pointer allows you to diverge from the main line of development, known as the main or master branch, and work independently.

The beauty of Git's branch model lies in its lightweight nature; branches are just simple references to commits, making them quick to create and switch between.

Creating a branch does not duplicate your project files. Instead, Git creates a new pointer in the repository that references the same commit as the current branch. This means that you can start making changes immediately without worrying about affecting the original codebase.

Creating a Branch with git branch

The most straightforward way to create a branch is using the git branch command followed by the name of the new branch. The syntax is as follows:

For example, if you want to create a branch called feature/login, you would run:

After executing this command, Git creates the new branch but does not switch to it. You will still be on your current branch. To verify that the branch has been created, you can list all branches with:

The output will show the newly created branch alongside existing ones:

The asterisk indicates your current branch.

Creating and Switching in One Step

If you want to create a new branch and switch to it immediately, you can use the -b option with the git checkout command:

For example:

This command accomplishes two tasks in one go: it creates the feature/login branch and checks it out, meaning you are now actively working on that branch.

With Git version 2.23 and later, the git switch command provides a more intuitive way to achieve this:

This command provides clarity by indicating that you are not only creating but also switching to a new branch.

Remote Branch Creation

Creating branches is not limited to your local repository. You may also want to create branches in a remote repository, especially when collaborating with a team.

To create a remote branch, you first create the branch locally and then push it to the remote repository:

The origin refers to the default name for your remote repository. After executing the above commands, the branch will appear in the remote repository, allowing others to see it and work with it.

To set up tracking for the remote branch, you can also use:

The -u flag sets upstream tracking, making it easier to push or pull changes in the future:

These commands will now default to the feature/login branch on the remote.

Best Practices for Branch Naming

When creating branches, following a consistent naming convention can significantly improve collaboration and workflow. Here are some widely accepted practices:

  • Use Prefixes: Use prefixes like feature/, bugfix/, or hotfix/ to categorize branches. This immediately signals the branch's purpose.
  • Short Descriptive Names: Keep branch names concise but descriptive. Instead of feature/add-user-authentication, consider feature/auth.
  • Use Issue Numbers: If your team uses an issue tracker, consider including issue numbers in branch names, e.g., feature/123-add-auth.
  • Avoid Special Characters: Stick to alphanumeric characters, dashes, and underscores. This avoids issues with various tools and scripts.

Following these conventions not only helps you but also aids your teammates in understanding the context of your work.

Now that you understand how to create branches and the importance of naming conventions, you are ready to explore switching branches. In the next chapter, we will look at how to navigate between branches with confidence, ensuring you can move your development focus wherever it's needed.