Skip to main content

How to Create a New Branch in Git | MakeUseOf

Branches are central to the concept of version control in programming, and Git in particular. This starter article tells you what a branch is and how to create one using a number of different tools.

What is a Git Branch?

In version control systems, the term branch is used as an analogy with trees in the sense that each branch emerges from another, eventually ending up back at the trunk. Branches allow you to create individual lines of development, in order to work on them in isolation without disturbing other work.

Related: How to Use Git Branches to Structure Your Programming Project

Using Git, you’ll be working on the master branch by default, whether you’re aware of it or not. This is often referred to as your active, current, checked-out, or HEAD branch. At any time during your development cycle, you can create a new branch and carry out separate work in each branch, from that point onwards.

Creating a New Branch on the Command Line

The command-line Git program offers the most power and flexibility, but there’s a lot to learn. If you’re comfortable digging around the man pages and make heavy use of Git, it’s a great option.

Use the git branch <branchname> command to create a new branch with the given name:

$ git branch dev
Branch 'dev' set up to track local branch 'master'.

This branches from the current branch, so make sure you’ve switched to the one you want to branch from before you execute that command.

You can list all branches and confirm the new one has been created using git branch without any arguments:

$ git branch
1 dev
2 * master

You can see more information, including which branch another one tracks, using the -vv flag:

$ git branch -vv
1 dev d1a9e5b [master] commit comment
2 * master d1a9e5b commit comment

If you try to create a branch before the first commit, you’ll get an error message like:

fatal: Not a valid object name: 'master'.

If you try to create a branch using a name that already exists, you’ll get an error message like:

fatal: A branch named 'dev' already exists.

The git branch command creates a new branch pointing to the same commit you’re currently working on. However, your working copy will still be pointing at the master branch. To switch to the new branch you just created, use git checkout:

git checkout dev

The term checkout might be confusing if you’re used to other version control systems; in Git, checkout refers to switching the currently active branch. Since you’ll usually want to switch to a new branch once it’s created, there’s a shortcut for the whole process:

git checkout -b dev

That command means “create a new branch called ‘dev’ and switch to it immediately”. It’s the equivalent of:

git branch dev
git checkout dev

In fact, you can even use git checkout to create a branch from any other, not just the one that is currently checked out. For example, to create a new branch called another, from the branch named dev:

git checkout -b another dev

Creating a New Branch Using GitHub Desktop

Another way to create Git branches on Windows or macOS is using GitHub Desktop, the official graphical user interface (GUI) program provided by GitHub. Using a GUI is perfect for beginners, and those who have nightmares when someone whispers the word Vim.

GitHub Desktop will always show your current branch in the main toolbar:

Click on that main toolbar button to show details of the repository’s branches, including the option to create a new branch:

Note that, if you start typing a branch name with no matches, GitHub Desktop prompts you to create a new branch and shows the keyboard shortcut to do so—useful if it’s really the kind of thing you find yourself doing a lot:

You can also start by pressing the New Branch button immediately. Whichever route you take, you’ll end up with a dialog to confirm the new branch name:

Your new branch will always be based on whichever branch was active when you created it. GitHub Desktop will switch to your new branch which will automatically track the branch you created it from.

Creating a New Branch Using Tower

Other GUIs are available from third-parties. Tower is free for a 30-day trial period and is available on macOS and Windows.

To create a new branch from the currently checked-out branch, select Create New Branch from the main Repository menu:

To create a new branch from any available branch, right-click on the branch in the left-hand sidebar and select Create New Branch from <branch name>:

Note that, in either case, you can enable the branch as a tracking branch, or change the Starting Point to any branch available:

Creating a New Branch Using GitKraken

GitKraken is another popular GUI that can seem intimidating at first, but it does a good job of visually representing key Git concepts, including branches. GitKraken is free for open-source use and is available for Windows, Mac, and Linux.

Make sure you’re working with the correct active branch; it’s the one highlighted in the branch listing in the left-hand sidebar:

To create a new branch, click the branch icon in the main toolbar:

Enter your branch name and hit ENTER:

The new branch will automatically be checked out and you'll receive a notification on the right-hand side of the screen.

Creating a New Branch on GitHub

As an alternative to running a local app, you can host your repository on one of two popular Git-supporting web apps. The first, GitHub, is a very popular option with the open-source community.

GitHub displays your current (active) branch in your repository view, near the top-left:

Click the button to display existing branches:

Type the name of your new branch and note that you are given the option to create it from the current branch:

Once created, your new branch becomes active.

Creating a New Branch on Bitbucket

Bitbucket is another popular version control web app that offers free accounts with an unlimited number of private repositories.

From any page within your repository, select the Branches item from the menu on the left:

Click the Create branch button in the top-right. Enter the new Branch name and click Create. If you need to branch from anywhere other than master, change the From branch first:

Bitbucket makes it easy to select a Type which is a prefix added to the branch name that can encourage a more organized approach to branches. It’s just a convention, rather than a built-in Git feature, but it can prove useful.

Once created, Bitbucket displays a view of your new branch:

Bitbucket screenshot showing new branch pageLearn to Branch Out With Git

Git is all about branches: they’re cheap to create and they allow multiple streams of work to coexist, ready to be merged when complete. Once you’re familiar with creating, switching, and merging branches, you’ll be well on the way to using Git to its full potential.

Comments

Popular posts from this blog

The Best 10 Social Media Platforms for Photographers to Flaunt Their Talent

Social media offers an excellent opportunity for photographers to connect with potential clients. In the digital era, it's a great asset. By showcasing your work on these networks, you can reach new audiences. Whether you are a professional or freelance photographer, the following social platforms will help you show off your work and get the right people to take notice... 1. Behance Behance is a classic portfolio publishing network that functions like a LinkedIn for creatives. Designed by Adobe, this is one of the best photography networking sites currently out there. The platform is ideal for sharing your portfolio and favorite images, allowing other Behance users to like and comment on your photos. By learning from their feedback and professional critiques, you can improve your work. The coolest feature of Behance is that it lets you find professional gig opportunities right on the platform. With your portfolio already available on the site, getting work becomes effortless.

The 6 Best Platforms for Sharing Your Digital Art Online

Whether you're looking for somewhere to host your digital art portfolio or simply want to share your latest artworks, it can be difficult to choose a website to upload to. Or at least, it definitely is more so than before, now that art websites aren't bubbling with as much excitement as they used to be. You know that each site has its pros and cons, but it's hard to figure out what those are unless you make an account and see for yourself. Don't worry if you don't have time for that—we've got your back. Here are the websites we recommend for sharing digital art, and why you might want to consider them. 1. Pixiv If you were around when the online art scene was ridiculously active, chances are that your art style is influenced by anime and/or manga in some way. Otaku culture began its slow sneak into mainstream media back then, and Pixiv is a great home for artists that fall in that category. Pixiv started as a small online community based in Japan, but has s

Snapchat Suspends Two Anonymous Messaging Apps Over Cyberbullying Claims

In light of a lawsuit that was filed earlier, two Snapchat apps, Yolo and LMK have been suspended by Snap. The apps allowed users to send anonymous messages on the platform. The Lawsuit Calls for an Immediate Ban of Yolo and LMK According to a LA Times report, the lawsuit was filed on behalf of Kristin Bride, the mother of a teen who committed suicide in 2020. The lawsuit alleges that Bride's son took his own life after being cyberbullied via Yolo and LMK. In addition to this, the lawsuit alleges that Yolo and LMK aren't doing enough to tackle cyberbullying, and have consequently violated consumer protection law as well as their own terms of service and policies. Both apps use Snap Kit, a set of tools that allows developers to directly connect to Snapchat for better integration features. Today the family of a 16-year-old Oregon boy who took his own life after being cyberbullied sued Snap and the makers of apps YOLO and LMK, alleging that the companies should be "h