Create Branch

Last updated on

A Git branch is a lightweight movable pointer to one of your commits. A feature or fix is thought of as a separate stream of development that you can work on without affecting code in the mainline. You can always create a separate branch and make changes of your own, without tampering with the actual code which will still remain intact until you decide to merge the work done.

In this tutorial you will understand how to create a new branch on your repository.

Create a New Branch

Git Branching is a space that Git gives to you for experimentation, adding features and changing without screwing up the production code. Every branch has their own story and can be then converge with other branches. It’s a simple concept in Git that improves the workflows of teams.

Here is a diagram shows you how the branch is added to the line of development.

Git Create a New Branch

You can create a new branch with the following Git command.

git branch branch-name

This command will create a new branch but is not in it yet. Suggestion: You can create the new branch and switch to it in one line.

git checkout -b branch-name

In this diagram, you will learn how this command pushes the new branch into the repository.

Create a new branch

Moreover; while creating a new branch, all you need to remember are the few steps below only so that the branch name is understandable and straightforward. Well, the next section will learn you how to select good branch name.

Considerations for Naming Git Branches

Clear names are important so other people (or even you in a few months) can figure out what the heck this branch is all about. Here are some best practices:

  • Lowercase Letters and Hyphens: Stick with lowercase letters by using hyphens instead of spaces. Eg: feature-login-page or bugfix-header-error.
  • Prepend Purpose: Prepend feature, bugfix, or hotfix to your branch names. This clarifies the branch and what it is doing. Such as feature-user-authentication or bugfix-footer-alignment.
  • Keep it Descriptive but Short: While it’s important to be descriptive, try to keep the branch name concise. A branch named feature-add-user-authentication is better than feature-to-add-user-authentication-module-to-the-application.

If you follow these conventions, your Git history will be more legible, and your repository clean and organized so that anyone working on the project can navigate it without too much difficulty.

Creating a New Branch Over a Specific Commit

Using the git branch, you can create different branches on a particular commit like below This scenario will create a new branch that will point to the above-specified commit so this way it is created at that part of the project history.

Here is the command:

git branch <new-branch> <commit-hash>

By running this command git reflog, you can view the commit history and identify the commit you want to use as the base for your new branch.

git reflog

When you have found the commit, make a new branch based on it with git branch followed by SHA of the commit.

For example, let’s say we want to create a new branch based on the commit with the SHA 70c5b73. To do that, just run the following command.

git branch new-branch-name 70c5b73

This command creates a new branch called new-branch-name based on the commit with the SHA 70c5b73.

git create a new branch

You can move to the new branch with git checkout and begin hacking as you normally would.

Another way to think of it is that this command displays the entire rotating timeline (history) state of refs, and then you can reference a specific commit in order to consist on your new branch.

Let’s move into the following part to learn how to create a new branch based on a tag in the workflow.

Create a New Branch From a Tag in Git

Note that you can do the same in Git, creating a branch from any tag. It is also so much useful when you wish to work on a specified version of your codebase. You can create a separate branch from there windows itself by issuing git checkout and adding flag -b with new-branch name after that tag name as given below:- Here’s an example:

git checkout -b new-branch-name tag-name

This command creates a new branch new-branch-name from the commit pointed to by tag-name and checks it out. From here, you can make modifications and commit them accordingly.

If the commit you would like to turn into a branch does NOT have a tag name matching that commit, an error will be shown. This means you should first create a tag or have the right name for that tag before trying to make it into a branch.

To list all available tag names in a Git repository, you can use the following command:

git tag

This will display a list of all tag names in alphabetical order. If you want to see the tags sorted by their commit dates, you can use the following command:

git tag --sort=-committerdate

Anyway, after you understand what branching is, let’s move on to the next part, which addresses the conflict issue related to merging in Git.

Let’s summarize it.

Wrapping Up

One of the most powerful features in git is branching, allowing developers to run on different functionalities on a project simultaneously. This really comes in handy when working as part of a team – well so that you don’t step over each other’s toes 🙂 Basically branching is a way that allows you to create isolated changes without affecting the main line of development.

The basic command to create a new branch in Git is straightforward:

git branch new-feature

It is important to say that this creates a new branch but does not move into the created one. For developers to do actual work on the newly created branch, you need another command for git checkout.

One scenario when working with branches is creating a new branch at a specific commit. The next instruction does it;

git branch <new-branch> <commit-hash>

Remember, the git reflog command allows you to see the history of refs such as branch checkouts, commits being made, and so on. This history helps you find the commit that should be used as a base of the new branch.

To create a new branch based on a tag, developers can use the following command:

git checkout -b new-branch-name tag-name

This will create a new branch called ‘new-branch-name’, on the tag referenced by the name of the tag, and check out that branch. This is important to make sure that a branch will be created from the right tag.

Thank you for reading. Heppay Coding!

Frequently Asked Questions (FAQs)

  • What is branching in Git?

    Branching in Git is the process of creating a separate stream of development from the mainline, allowing you to work on features or fixes without affecting the main codebase. It creates a lightweight pointer to one of your commits, letting you experiment and make changes freely.
  • How do I create a new branch in Git?

    You can create a new branch in Git with the command:
    git branch branch-name
    To create and switch to the new branch in one step, use:
    git checkout -b branch-name
  • Why is naming branches important?

    Naming branches clearly helps maintain a clean, organized repository. It ensures that others (or even yourself in the future) can easily understand the purpose of each branch, avoiding confusion in collaborative development.
  • What are the best practices for naming branches?

    Best practices for naming Git branches include:
    - Using lowercase letters and hyphens, e.g., feature-login-page.
    - Prepending the purpose, like feature- or bugfix-, to clarify the branch's role.
    - Keeping branch names descriptive but concise, e.g., feature-user-authentication instead of a long, complex name.
Share on: