How to Create a New Branch on GitHub‍

Table of Contents

How to Create a New Branch on GitHub

One of Git's fundamental ideas is branching, which enables one to work on a specific feature without impacting other code. If you use Git, you've undoubtedly made branches a lot, but there are a few different methods to accomplish so.

This allows developers to make changes securely before deciding whether to discard them or incorporate them into the rest of the project. Knowing these various approaches and their benefits may make working with Git easier. Having a clear workflow is crucial to maintaining your Git repository, and it includes how and when you generate branches. Your project may end up in disarray if there are too many of them or if they aren't created at the appropriate time. Your branching approach is important because of this.

In this post, you'll learn more about Git's data architecture and how to create branches using the command line, the GitHub web interface, or orphan branches, as well as the benefits of each technique.

Git Logs and Data Model

Git records every modification you make to your code. Your commits create a continuous history of your project that can be inspected and revisited.
This allows you to go back in time and examine what modifications were done. By doing so, you may go back and review the modifications that were made during an earlier stage of development.

Users can inspect the commit record by typing:

git log

That will show all your commits. You can shorten it by adding the number of commits you wish to see as a parameter. To view the last ten commits, for example, type:

git log -20

The Git Branch Command

The command line is the most often used method of working with Git. Except in the case of orphan branches (more on that later), new branches are normally based on existing ones.

To establish a new branch, open the command line, which will be Bash on Mac and Linux or the command prompt on Windows. Before you begin, navigate to the folder containing your repository.

This is the easiest way to do it: 

git checkout -b 

That creates the branch and switches straight to it. If you don’t want to switch to it, run:

git branch 

and switch to the new branch later with:

git checkout 

This approach is simple and has no significant drawbacks. If you use it locally, your changes will not be visible to others until you push to the remote branch, so if you delay too long before updating it, your version of the repo may deviate from the master copy.

Try typing:

git checkout -b  

to generate a branch based on an existing one.

Branches can also be created based on certain tags or commits. To employ a tag, enter:

git checkout -b  

Or for a commit via:

git checkout -b  

where symbolic_reference is replaced by something like HEAD~3, or a reference to the commit you want to branch from.

The DAG

Git creates a log of changes to your repository using objects and references. Objects include particular commits, trees that refer to objects, and blob files that describe your files.

They all flow in the same way and may be seen as a directed acyclic graph, or DAG. Branches and tags, both of which refer to commits, are examples of references.

Here is a pretty simple command-line representation of the DAG you can try:

git log --graph --decorate --oneline

GitKraken, for example, can generate visuals for you. As your repo becomes more sophisticated, having a visual perspective of where you are and where you've been will assist you in determining your future steps.

Orphan Branches

Sometimes you want to share a branch with someone but don't want them to view the complete project history. A fresh orphan branch is an answer since it is generated from scratch with no commit history. 


Create it using:

git checkout --orphan 

Additionally, if you wish to work separately or start again with a fresh branch, this is helpful. Perhaps you're satisfied with the project's prior history and wish to start a fresh log here.

Other uses of orphan branches combine numerous repositories, establishing a separate documentation branch, or building a branch for a static site are all examples.

The GitHub Online Interface

GitHub's browser interface allows you to execute the most frequent actions on the site. Go to the project website to start a new branch. At the upper left, you'll see the branch you're presently in listed—usually, master or main.

If you click it, it will expand and reveal a text area. There will also be a list of existing branches, which will only include the one branch you created before creating others.

In the text area, type the name of the new branch. You'll see text below detailing what will happen, such as "Build branch: foo from main." Press Return.

If you wish to branch from an existing branch, just switch to it before establishing the new one. You may also create branches from specified items, such as commits or tags. If you select the Tags tab next to the list of branches, you'll get a list of possible tags to use. GitHub's site is quick, accessible, and user-friendly, making it a fantastic alternative for users who aren't yet familiar with the command line. It gives you a lot of information at a look, so you can clearly see your possibilities.

This is a good place to start if you're new to Git. You may experiment painlessly by making a fake project.

Other Git Branching Options

Git can be managed in numerous ways. There are desktop and mobile applications for GitHub. You may browse the commit history using these, which include all the functionality you'd anticipate. You also get helpful code diffs that let you quickly see what has been added. This method of browsing your team's code can help you keep track of everyone's contributions.

Git can also be used from a web-based interface on other websites, including GitLab. These have grown in popularity and provided solid alternatives to the bigger platform after GitHub was purchased by Microsoft.

Git support is available in many IDEs as well, enabling branch operations. Additionally, Git is supported by NetBeans and all of its various offshoots. In the event that your IDE doesn't natively support Git, it could have a plugin that does.

Which Branching Strategy to Use

Now that you know how to make branches, think about when to use them to make the most of your flow strategy. Git flow is a primitive technique that uses many branches for distinct objectives.

It integrates nicely with conventional release models and enables you to plan the development of features and group them for particular releases.

A more recent option is GitHub flow, which seeks to make things simpler by employing feature-specific branches that are removed after merging. Small teams who wish to compartmentalize their work without adding too much complexity would love it. As your team expands, though, things could become complicated.

In order to give you the best of both worlds, GitLab flow has fewer branches than Git flow but more branches overall to accommodate scenarios that GitHub flow struggles to handle.

Which is ideal for you will depend on the size of your team and the basic procedures you have in place. For larger projects, the extra release branches and optional staging and production branches help you manage things efficiently.

Conclusion

Branches are an effective design element. You can divide work across your team more safely if you know how to build them and when to do so. You can divide work across your team more safely if you know how to build them and when to do so. 

Your project will be simpler to work on and your repo structure will be more consistent if you can persuade everyone to cooperate better. Your repo's use will be optimized if you employ a specific branching strategy as part of a specified Git workflow.

Ottomatik can help you keep your data secure and swiftly restore your repositories if you do need assistance with Git. It's a dependable, cost-effective solution to ensure the availability and safety of your code. Granular recovery capabilities are offered via daily automated backups, which also include information like commits, tags, and more. Try Ottomatik and find out more about how you can make your database management much more easier.

Ready to secure your backups today?

Try for free
14 Day Free Trial • Cancel Anytime • No Credit Card Required