Git and GitHub: The Complete Guides - Chapter 4: Branches
Tags: github, git, webdev, beginners
Published: 2021-09-11 12:07:26

![Goran-hi-friend](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7nibchqo9rpy0i3ks35u.png)

Welcome to this chapter, we are going to discuss one of the important features of Git which is called **branching**. In Git branches is part of your everyday development process. For example when you want to add a new feature to your project or maybe you might need to fix a bug, then you can use **branching**.

**Here we will cover how to:**

<ul>
<li>Create a branch</li>
<li>Create and switch to a new branch</li>
<li>Delete a branch</li>
<li>Push branch to Github</li>
<li>Pull branch from remote to local</li>
<li>Delete a branch</li>
</ul>

![branching](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ppibreuioamrdj8z7pqs.gif)

As shown in the above diagram, we have a project timeline with different commits. This time-line is actually a branch. By default when you create a repository and make commits, you are working on the *master* branch. This is what we have already seen in previous chapters.

Continuing with our example, sometimes you need to add a new feature to your project, but at the same time you don't want to touch or break the current state of the project. In that case the best solution is to create a new branch and then work on that newly created branch <u>(adding files, making commits etc..).</u>

Then after creating and testing the new feature, if you are happy with it, you can then merge your branch to the master branch. **You are able to create as many branches as you wish.** 

It becomes really helpful when several developers are working on the same project, each of them can work on a different branch without breaking another developers work.

![project-branch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d7v1z1qonptn921j97bg.gif)

<h2>Create a branch</h2>

Lets go and see in practice how to work with branches, We go back to our project, where we are working on the default master branch. To create a new branch we have to run the following command: ```
git branch 
```

This command is followed by the name of the branch. For example: ```
git branch feature-1
```

In order to see what branch we are working on and to see how many branches we have, we need to run the following command: ```
git branch
```

Now we can see overall we have two branches and currently we are working on the *master branch, as indicated by the asterisk and green-colour text.

Lets see how we can switch to a different branch, for that we have to run the command: ```
git checkout feature-1
```
Now we will be working on the feature-1 branch. If we check the commit history, we will get all the commits we have made so far. We are able to change these commits and also make new ones, which won't be available on the master branch.

![git-branch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kwypp3ggl60q9rqgbobx.gif)

If you remember from previous chapters, we can switch back to our master branch by running the following command: ```
git checkout master
```

Note that changes or commits we make on our branch will not be seen by the master branch and we can prove this by making a new commit on our feature-1 branch and then checking if the commit is tracked on the commit history on the master branch.

![git-branches](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hf14asf9l1oh43lxr7hj.gif)

<h2>Create and switch to a new branch</h2>

![switch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bpgmfqqtc1j08pk7edeh.gif)

Lets move on, time to see how we can create and switch to a new created branch with just one command. Suppose we want to create a new branch called feature-2 and switch to it immediately. To do this we need to run the following command:
``` 
git checkout -b
```
This command is followed by the name of the new branch. For example: 
``` 
git checkout -b feature-2
```

Now when we run `` git branch `` we will see we have three branches and we have switched to our new branch as well.

![branch-immediately](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3cns1s296xf7wz9ch3eo.gif)

<h2>Delete a branch</h2>

Next we have to discuss how to delete branches. To do this we need to run the following command: ```
git branch -D
```

This command is followed by the name of the branch we want to delete.

>Remember we cannot delete the branch which we are currently working on, if we try to delete a branch we are currently on, we will get an error that says we can't delete it.

Once we delete our branch we can see how many branches we have left with the command ``git branch``

![branch-delete](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ygv6rxy8nkju1mjmu46.gif)

<h2>Push branch to Github</h2>

We can push our example feature branch to our remote repository by running the following command: 
``` 
git push origin feature-2
```

To do this we first have to make a commit on our branch, which can been seen here.

![push-branch-Github](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ejsi0e0dfp4lbfzjs3s.gif)

When we push our local branch to GitHub, we can see that our branch has been added, we can then go to our branch and see the commits that were made. GitHub will even tell us by how many commits we are ahead of our master branch.

<h2>Create a branch from GitHub</h2>

We can also create a branch from the Github website. It is pretty simple. We click the button that says **master** and give a name for our branch, then click create branch. We can see our new branch has been created. 

We can even go to the **branches** button to see all our branches. When we hover over the numbers, we can see by how many commits our branch is ahead of the master.

![branch-Github](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4w6hhsp9p4m5ioyb3gx7.gif)

<h2>Pull branch from remote to local</h2>

If we created a branch on GitHub and we want to work with that branch on our local repository, we have to pull the data from our remote to our local. To do this we run the following command: ```
git pull origin
```

Then we follow the command with the name of our remote branch. For example: ```
git pull origin feature-1
```

Then we need to run the command: ```
git checkout --track origin/feature-1
```

Now we have pulled the data from GitHub to our local repo and we have switched to it. We can check this by running the command: `` git branch ``

We can make a change to our remote branch on GitHub and make a commit to it. But to have access to the changes, we need to pull the data from the remote branch with the command: ``git pull origin feature-1``. 

![Github-pull](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mse7dwu6ttvz3obt51b8.gif)

>You may have noticed Git showing us that our pull request is not really the best method. This is true. We haven't covered some of the topics yet so ignore it for now.

We can then check the commit history to see that our commits are up to date with the remote branch.

![git-log](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8tu9egyfnr9zoe7ysgt5.gif)

Each time when we make a new commit on the feature-1 branch on GitHub, remember we have to pull it to our local repository,  by running the command `` git pull origin feature-1``

<h2>Delete a branch</h2>
We can delete a branch on GitHub by going to our branches, click the branches button. On the right side of our branches we can see the little recycle bin, which when clicked allows us to delete a branch.

Now when we go to branches we won't see our branch anymore. We can even delete our remote branches from our local repo by running the following command: ```
git push origin --delete
```
This command is followed by the branch we want to delete. For example: ```
git push origin --delete feature-2
```
![GitHub-delete](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i1tksnwbwue6dncs62uf.gif)

By going back to GitHub we can see this branch has been deleted. But deleting it this way does not mean it is deleted on our local repo, in fact if we check to see how many branches we have on our local repo we will see all our branches are still here.

We know how to delete a branch on our local repo, we do this by running the command: ``git branch -D `` followed by the name of the branch we want to delete.

>When deleting a branch you would usually use ``git branch -D <file> ``. This is fine, but notice the -D flag forces the branch to be deleted. Instead use the -d flag, This way Git will protect from deleting a branch that has not been merged yet and causing you to permanently lose your work.


We can check our local branches with the command: ``git branch`` which will show us only the master branch.

![git-delete](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gjrjx5sh1bmi44tthx8h.gif)

That concludes this chapter on branches, however we will be discussing more about branches in the coming chapter. I hope you enjoyed it. See ya!

![thor-wink](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ks5wjmy35cqcw6wx28j.gif)