3) Collaboration with Git & GitHub Lesson

Git Branch

26 min to complete · By CodingNomads Team

So far in this course, you've seen many mentions of "branches" in Git. Now it's time to finally dig into the git branch command.

In this lesson, practice using common Git Branch functions including Git Branch -A, Git Create Branch, Git Checkout, Git Push Branch, Git Delete Branch, Git Rename Branch, Git Merge, and more.

What is a Git Branch?

A Git branch represents a divergent line of development that stems from the working tree at a certain point in time, allowing for parallel development. When you hear the word branch, you might think of real-world trees, and you can envision your Git branches alike.

  • A tree has a trunk and many branches
  • A Git repository has a default branch (the trunk) and many side-branches

The big difference is that Git branches often reconnect to the default branch, which is something that would look rather strange in real-world trees. A Git repo is typically visualized a bit more like this:

Git Branches Visualization

Types of Git Branches

Default Branch

The trunk of the project, here called "Master"

Release Branch

This preserves a snapshot of each release of your software

Development Branch

This acts as a collaboration branch to merge features and cut releases. Now, in a sentence: When the feature is complete and tested, it is merged into the Development Branch, which is then merged into a Release Branch which is then merged into the Default Branch. Then there are also:

Two Feature Branches

These are short-lived branches that are dedicated to the development of one or more specific features.

Local Branch

Local branches are branches that only you can see, and they exist only on your local machine.

Remote Branch

Remote branches are branches on the "remote". You can push and pull branches to and from your local machine and your remotes.

Colorful illustration of a light bulb

Info: There has been sustained discussion about changing the naming of the Git default branch from master to a different word that doesn't have negative connotations. At the time of writing, GitHub has changed its default branch naming to main.

However, It is easy to change your default branch name on your local Git. Ccheck out how to change a branch name further down in this lesson.

Common Git Branch Commands

1. Git Branch -A

You can use the command git branch -a to view all branches that are currently available to you. In a new empty repository cloned from GitHub, you will see by default a single branch named main. You will also see an asterisk * next to it. The asterisk indicates that this is the active branch:

* main

Below, you will see an example of a project that has multiple local and remote branches. Notice that the branch main has the asterisk next to it. There is another local branch named Testing, and there are three remote branches, Resources, Testing, and main. Notice that HEAD simply points to main.

Output of git branch -a command
Colorful illustration of a light bulb

Info: Using the -a flag will give you a list of all remote branches in addition to the local branches. Without -a, you'll only see the local branches.

The git branch and git branch -a commands can be helpful to get an overview of which branches exist in a project. These Git commands really come in handy when you are working on a large and active project with multiple collaborators.

2. Git Create Branch

A branch is really just a pointer to a specific commit, a specific moment in time. When you create a branch, Git creates a new pointer to a specific commit. Creating a branch doesn’t affect or change your code in any other way.

To say this in different words: it gives you a copy of the code from that specific moment in time, which you can modify however you see fit without affecting the branch you branched off of.

Colorful illustration of a light bulb

Info: You can create a branch off of any branch, not only from your project's default branch

There are a couple of ways to create a new branch. You'll start by looking at the git branch command. As mentioned before, you can list all branches with this command as well:

$ git branch -a

To create a new branch, you need to enter a name for the new branch at the end:

# git branch <NEW_BRANCH_NAME>
$ git branch my_first_branch

If you go ahead and list the branches again after running the above command, your output will look like this:

* main
my_first_branch

Your my_first_branch branch has been created. Notice, however, that the asterisk is still next to the main branch. Although the new branch was successfully created, the main branch is still the active branch. To switch to the new branch, you need to use a different command. You'll look at this one next.

3. Git Checkout

To switch from branch to branch, you use the git checkout command. Assuming your setup from before, you can switch to your my_first_branch branch like so:

# git checkout <BRANCH_NAME>
$ git checkout my_first_branch

Your chatty friend Git will let you know what just happened by sending you the following message:

Switched to branch 'my_first_branch'

After switching to my_first_branch, you can do all the work you need to do without worrying about messing up anyone else's work on any other branch. Everything you do, all the files you change, and all the commits you make will be confined to this specific branch.

When you are done with your work, and after you test your work, you can merge my_first_branch back into the main branch. Following the standard Git workflow, you would then delete my_first_branch and create a new branch to work on your next task.

Colorful illustration of a light bulb

Info: A faster and more efficient way to create a new branch and check that branch out with only a single command is git checkout -b <NEW_BRANCH_NAME>.

This command will create the branch and immediately switch to the new branch. Switching to a branch is also called checking it out, hence the name of this command.

Now that you know how to create a new branch and use git checkout so that you can do your work there, next you'll learn how to keep your work in sync with your remote.

4. Git Push Branch

The branch my_first_branch that you've created lives in your local Git repository. If you want to push this branch to a remote repository, you can do this using the git push command:

# git push <REMOTE_NAME> <BRANCH_NAME>
$ git push origin my_first_branch

Here, you're pushing the newly created local branch to your remote origin under the same branch name, my_first_branch, as it is also called locally. Pushing your branch to GitHub like this recreates the local branch also on your remote.

5. GitHub Create Branch + Git Fetch -A

You can also create branches manually on GitHub. However, this is not something developers normally do. Since it is possible, here's a look at it anyway:

Creating a Branch on GitHub Manually

In order to locally use the new branch you created on GitHub, or one that a peer has pushed to GitHub, you need to use the git fetch -a command. This command will fetch all the remote branches to your local machine:

$ git fetch --all
# or 
$ git fetch -a

If you run git branch -a after the previous command, you will notice that the branch you created on GitHub is now also visible on your local machine. To fetch and merge the remote branch into your current working directory, use the git pull command:

# git pull <REMOTE_NAME> <BRANCH_NAME>
$ git pull origin myNewBranch
Colorful illustration of a light bulb

Note: For review on how to use git pull and how it differs from git fetch, check out the previous lesson on Git Pull. Give it a try and answer these questions:

  • What does pulling a new remote branch do, and how is it different from fetching the branch?
  • Does it automatically merge into the branch you are currently on?

Now you might have gone and created one too many branches. So it's time to learn how to delete a branch that you don't need anymore.

6. Git Delete Branch

As a software developer, you will delete branches frequently. As soon as you're done with a feature branch, for instance, you should delete it. There is no use in keeping old branches around, since all the changes that were made in this branch have now become part of the main codebase. After that, old branches only clutter up your Git repo. Below is how you use Git to delete and local branch, and delete a remote branch on Git.

Git Delete Local Branch

To delete a local branch, use the git branch command and add the -d flag, which tells Git that you want to delete the branch with the name you put after the flag:

# git branch -d <Branch Name>
$ git branch -d my_first_branch
Colorful illustration of a light bulb

Note: This command will only succeed if you are not currently on the branch you are trying to delete. If you are on my_first_branch, switch first to another branch.

Running git branch -d is a safe operation in Git. Git will prevent you from removing the branch if it has unmerged changes. Unmerged changes are any edits that have not yet become part of another branch.

If you don't receive an error message, then Git was able to delete the branch successfully. You can run the appropriate command to list all your branches and verify that my_first_branch is no longer part of this repository.

Git Delete Remote Branch

Just as you often need to delete local branches, you also need to delete remote ones. You can delete a remote branch using the git push command while adding the -D flag:

# git branch -D <Branch Name>
git push origin -D myNewBranch
# or 
git push --delete myNewBranch

Either of these two commands deletes a remote branch named myNewBranch. If using git push for a deletion event seems unfamiliar, keep in mind that git push is there to propagate changes to your remote. These changes can be additions or, as you see here, deletions.

7. Git Rename Branch

It is possible to change the name of your current branch in Git. The command to rename a branch is git branch -m. Take a look:

# git branch -m <Old Branch Name> <New Branch Name>
$ git branch -m master main

In the example above, you changed the name of master to main. Keep in mind that master is in itself not a special branch, and all the same rules apply to it as to any other branch, which means you are free to rename it as you wish.

Colorful illustration of a light bulb

Note: At the time of writing, Git's default branch name is still master. GitHub has already taken the step towards additional inclusivity and named their default branch main.

8. Git Change Master to Main

We encourage you to use main also as your default branch name on your local Git repositories. For any projects with existing commits, you can do this by renaming the branch as described above, with:

$ git branch -m master main

However, if you have just initiated a new repository and you attempt to rename it, you will run into an error. Give it a try and start by creating a new directory called test, then initialize it as a new Git repository:

$ ~ mkdir test
$ ~ cd test 
$ git init

Everything works as expected so far, and you now have a new empty Git repo:

Initialized empty Git repository in /Users/martin/test/.git/

However, if you attempt to rename a branch that doesn't yet have any commit, you'll run into this error:

$ git:(master) git branch -m master main

error: refname refs/heads/master not found
fatal: Branch rename failed

This happens because the master branch doesn't really exist yet. It gets created only once you've added and committed the first change to the repository. Create a file, then add and commit it:

$ git:(master) touch hello.txt
$ git:(master) ✗ git status

On branch master

No commits yet

Untracked files:
 (use "git add <file>..." to include in what will be committed)

	hello.txt

nothing added to commit but untracked files present (use "git add" to track)

$ git:(master) ✗ git add hello.txt 
$ git:(master) ✗ git commit -m "Initial commit"

[master (root-commit) ab7d5fa] Initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 hello.txt

Once there's at least one commit in your new repository, it means that the default branch, currently master in Git, exists and can therefore be renamed:

$ git:(master) git branch -m master main
$ git:(main) 

The rename was successful. When you now check for your existing branches in your project with git branch, you'll see the following:

* main

Now your new local Git repository is using main as the default branch name.

9. Git Merge Branch

The git merge command is used to merge another branch into the currently active branch. For instance, if you want to merge another_super_branch into the main branch, you first need to checkout the main branch:

$ git checkout main

Once main is your active branch, you can run the merge command to integrate the changes from another branch into main:

$ git checkout main
$ git merge another_awesome_branch

Summary: Git Branch

In this lesson you learned all about what a Git Branch is. A Git branch represents a divergent line of development that stems from the main repository at a certain point in time, allowing for parallel development. Below is a summary of the main Git branch commands covered in this lesson:

1. Git Branch -A Use the command git branch -a to view all branches that are currently available to you.

2. Git: Create Branch

$ git branch <NEW_BRANCH_NAME>

3. Git Checkout

$ git checkout <BRANCH_NAME>

4. Git Push Branch

$ git push <REMOTE_NAME> <BRANCH_NAME>

5. GitHub Create Branch + Git Fetch -A Create the branch on GitHub, then fetch and merge the remote branch into your current working directory using git pull:

$ git pull <REMOTE_NAME> <BRANCH_NAME>

6a. Git Delete Local Branch

$ git branch -d <Branch Name>

6b. Git Delete Remote Branch

$ git branch -D <Branch Name>

7. Git Rename Branch

$ git branch -m <Old Branch Name> <New Branch Name>

8. Git: Change Master to Main For more on the discussion regarding this switch towards greater inclusivity, you can read up on both Git's approach and GitHub's approach.

9. Git Merge Branch Use the git merge command to integrate the changes from another branch into the main branch.

$ git checkout <Branch_Name to merge into>
$ git merge <Branch_Name to merge from>

For more information on branching, check the Git Branching documentation.

Next, what if something goes wrong? Git is your friend in such a situation. It's time to dive deeper into using git merge and how to resolve potential merge conflicts in the upcoming lesson. But first, a video demonstration on Git Branch.