top of page

Creating and Managing Branches in Git

What Is a Branch?

In this lesson, you will learn what a branch is and how it is a central feature that allows git for making collaborative work convenient.

Why are branches useful?

Let’s say you are working on a project with a team. You’ve been working on a significant feature that requires a lot of changes to the codebase, and, all of a sudden, one of your team members tells you that there is a major bug, and you need to prioritize it and fix it.

You will find yourself in a confusing situation. Not only will you need to switch context completely by focusing on the new issue at hand, but where will you store all the code you have been working on for the unfinished feature? The bug has nothing to do with the unfinished feature. Fixing the bug along with the feature is going to create a lot of confusion for everyone.

You will somehow need to go back to a state or snapshot of the source code before you started making changes to it for the feature, fix the bug, and then go back to work from where you had left off. This scenario is precisely where Git branches can work in your favor.

Branches in Git

In Git, you start with the one, primary branch called the main. This name is the default, given to the branch the moment you create your very first commit.

All the other commits you make from that point on are made on the master branch. Git, however, allows you to create as many other branches as you like.

You can create a separate branch that diverts away from the master and continue to do your work from there. The changes you make from that point forward and the commits you create will only be reflected in this branch and will not affect the source code in the master branch.

When you decide to create a new branch, what Git essentially does is create a new pointer to the current snapshot or commit to your project.

Therefore, the master branch has its pointer, and when you take a new branch out from the master branch, your new branch will have its pointer separate from the master.



The ability to create different branches allows you to work freely on the source code without affecting any other person’s work or the actual code in the master branch.

All the changes you make will be restricted to your branch and will not spill over to alter things you do not intend to alter.

This also allows you to switch back and forth between contexts without having to worry about what will happen to each branch once you move to another. Git is super flexible in this regard and has made working in collaborative environments extremely easy and convenient.

Branches out of other branches

Git lets you create a branch from any other branch. A new branch doesn’t necessarily have to come out of the master branch.


You can branch out from any branch.


Creating a Branch

Learn how Git lets us create branches seamlessly and quickly.

We'll cover the following:

  • The git branch command

  • How to create a new branch

The git branch command

The git branch command is a useful, multi-purpose tool that lets us do a lot of different things. When used as is (without any other options appended), the command will print out all the branches present in the repository and point out which branch we are currently on.

git branch

Switching Between Branches

Learn how Git allows us to quickly switch between different branches. This lesson also discusses an alternative way to create a new branch with Git.

We'll cover the following

• The git checkout command

• Creating new branches with git checkout

◦ The -b flag

The git checkout command

Using the git branch command lets us create new branches seamlessly. However, the problem is that even though we get to create new branches, we still don’t actually know how to switch over to those branches and use them. The git checkout command lets us do just that.

Once you create a new branch (let’s call it new_branch), use the following command while still currently on the master branch, to switch over to that new branch:

git checkout new_branch

Renaming Branches

Learn how to rename a branch in this lesson.

You might find yourself in a situation where you have to rename the new branch you’ve created. Git provides a very convenient way to do so.

We will, once again, rely on the git branch command to rename an existing branch.

Let’s say you have been working on a significant feature. You want to add authentication to your application. The branch that you are making all the relevant changes to is called new_branch. The name new_branch is not related to what you are doing in any way. No one else will be able to identify what this branch is about, either. It is a better idea to call it something like authentication_feature.

Renaming a branch you are currently on

  1. Switch over to the branch you have to rename with the following command:

git checkout new_branch

  1. Use the git branch command appended with the -m flag to provide the new name:

git branch -m authentication_feature

Deleting a Branch

Learn how to delete a branch from your local repository in this lesson.

We'll cover the followingUsing git branch to delete a branchThe -d flag

Using git branch to delete a branch

We can use the git branch command to create, list, rename, and delete a branch. In this lesson, we will take a look at how we can delete a branch from our repository.

The d flag

The syntax for deleting a branch is fairly simple:

git branch -d <branch_name>

If you have a branch called new_branch in your repository and want to remove it, you will need to enter the following command in the terminal:

git branch -d new_branch

As always, to double-check that the deletion process has been executed successfully, you can rely on the simple git branch command to verify this. The branch that is deleted should no longer be listed in the output.

Note: You can’t delete the branch you are currently on. You will need to switch over to another branch and then delete it.

Git Stash

Learn how to use the git stash command to stash uncommitted changes in this lesson.

The git stash command

Often, you will be working on your separate branch and making some changes that you don’t want to commit just yet, but you will be required to switch over to another branch to do something else in between.

You don’t want to get rid of the changes but also don’t want to commit them either. This exact scenario is where you can use the git stash command.

git stash temporarily stores the staged and modified files in a kind of a cache, all the while making the working branch directory clean.

You could either stash all of the uncommitted changes you have made in the branch or stash individual files.

git stash

When to use git stash

Let’s say you are currently working on a branch called new_branch and you made some changes to the file1.txt. You can verify which branch you are on by using the command git branch and check which files have changed using the command git status.

Test this out in the terminal below. The output should already be visible to you.

Terminal 1Terminal Click to Connect...

Now, for some other task, you are required to move to the master branch. However, the changes to file1.txt aren’t ready to be committed yet, but you also don’t want to lose the changes.

Ideally, you want to switch to the master branch where the changes you have made to file1.txt do not exist. Once you are done working with the master branch, you want to go back to new_branch and continue working on the changes you had started.

Here is what you will need to do:

  1. Enter the command git stash while you are still on new_branch.

  2. Enter the command git checkout master to switch to the master branch.

  3. After you have done whatever you needed to do on the master branch, you will, once more, enter git checkout new_branch to switch back again.

  4. You want your changes to be present once again, so you will enter the command git stash apply.

  5. Enter git status to verify if your changes are back or not.

And there you have it. The uncommitted changes you had made are all back again in your working directory, and you will be able to continue with your work uninterrupted once more. You didn’t have to commit your unfinished work, and you were able to switch branches and still keep your changes! A win-win situation.

The expected output you should get if you test these commands out in the terminal.

Had you decided not to use git stash, Git would have prevented you from switching over to any other branch if the other branch had changes that would be overwritten with the new uncommitted changes (since the changes in the other branch would be out of context). You would have to do one of the following:

  • Commit your unfinished work, switch branches, do your work, switch back to new_branch, and revert the most recent commit.

  • Completely get rid of the changes you had made, switch branches, do your work, switch back to new_branch, and start all over again.

In case the altered file does not conflict with the branch you plan to switch to, Git will let you switch over without errors and the uncommitted changes would still be present in that branch as well.

 
 
 

Comments


bottom of page