DEV Community

Cover image for Git and GitHub: The Complete Guides - Chapter 4: Branches
Goran Kortjie
Goran Kortjie

Posted on • Edited on

Git and GitHub: The Complete Guides - Chapter 4: Branches

Goran-hi-friend

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:

  • Create a branch
  • Create and switch to a new branch
  • Delete a branch
  • Push branch to Github
  • Pull branch from remote to local
  • Delete a branch

branching

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 (adding files, making commits etc..).

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

Create a branch

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 
Enter fullscreen mode Exit fullscreen mode

This command is followed by the name of the branch. For example:

git branch feature-1
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

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

git checkout master
Enter fullscreen mode Exit fullscreen mode

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

Create and switch to a new branch

switch

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
Enter fullscreen mode Exit fullscreen mode

This command is followed by the name of the new branch. For example:

git checkout -b feature-2
Enter fullscreen mode Exit fullscreen mode

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

Delete a branch

Next we have to discuss how to delete branches. To do this we need to run the following command:

git branch -D
Enter fullscreen mode Exit fullscreen mode

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

Push branch to Github

We can push our example feature branch to our remote repository by running the following command:

git push origin feature-2
Enter fullscreen mode Exit fullscreen mode

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

push-branch-Github

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.

Create a branch from GitHub

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

Pull branch from remote to local

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
Enter fullscreen mode Exit fullscreen mode

Then we follow the command with the name of our remote branch. For example:

git pull origin feature-1
Enter fullscreen mode Exit fullscreen mode

Then we need to run the command:

git checkout --track origin/feature-1
Enter fullscreen mode Exit fullscreen mode

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

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

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

Delete a branch

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
Enter fullscreen mode Exit fullscreen mode

This command is followed by the branch we want to delete. For example:

git push origin --delete feature-2
Enter fullscreen mode Exit fullscreen mode

GitHub-delete

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

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

Top comments (2)

Collapse
 
wpsyed profile image
Syed Saadullah Shah

Goran. Rock !

Collapse
 
iizmotabar profile image
Motabar Javaid

Thankyou for such informative content, Goran. Loved the series!