Creating and deleting branches within your repository (Fundamentals of Git and GitHub)

As a programmer, one should always be responsible for making their code clean as far as possible. Sometimes when you are creating a backup on your PC, things can get messy and out of hand. There can be multiple backups for your files until you reach a stage where your further modifications are not required. First of all, let’s have a look at a situation in which things start getting messy.

For example, You have multiple features that are required in your application. First of all, you decide to work on the sign-up feature of your application. So, at first, you might be developing multiple UI (User Interface) for the Sign-Up feature of your application so, till you reach the stage in which you have finalized the UI (User Interface) you will have multiple back-ups of your application.

Then, you might start working on making the Sign-Up feature functional. So, in this process too, you will be creating multiple back-ups until the situation when you reach a stable version. Hence, with all these backup for just developing a particular feature of your application other features that are required for your application will be still remaining. Now, you can imagine how much messy your backup files can get once you start working on the other features.

Purpose of creating branches

Now, the above-mentioned situations are the conditions in which branches play a vital role. Creating branches with Git allows you to create a backup of your code as per the feature that is required for your application. Hence, you can create multiple branches according to the features that are required in your application. Furthermore, after you have developed a stable version of your application, you can then merge your currently developed branch with the master branch. This not only manages your code but also allows you to keep your master branch clean.

Note: If you are completely new to Git and GitHub then you can view the blog provided below to obtain knowledge about Git and GitHub.

Furthermore, if you are willing to obtain knowledge about uploading files in GitHub using Git Bash Terminal by creating a basic repository and adding files, you can have a look at the blog post provided below.

Step-wise procedure for creating and deleting branches within your repository

Now, after getting to know the applicability for creating branches let’s look onto some of the Git commands in order to create, delete, and merge branches.

Before we look onto the step-wise procedure first of all let’s create a repository in GitHub as shown in the image below. For now, a repository named ‘RepositoryDemo’ has been created.

Image for post
Image for post
Creating a repository named ‘RepositoryDemo’

Now, go to your file explorer, then right-click and click on ‘Git Bash Here’ as shown in the image below.

Image for post
Image for post
Opening ‘Git bash’ in the required file explorer location.

Here, first of all ‘touch’ command is used to create a file named ‘index.html’. After you have executed the ‘touch’ command, the file can also be viewed from file explorer. Then, ‘ls’ is being used to display all the files present in the current directory.

Note: If you are new to shell commands and want to learn some of the basic shell commands, then you can view the blog provided below.

Image for post
Image for post
Using ‘ls’ and ‘touch’ command.

For now, let’s push the ‘index.html’ file to our remote repository.

Note: To obtain guidance for pushing your file to the remote repository, you can view the blog post provided below.

Image for post
Image for post
Viewing the recently pushed file i.e. ‘index.html’ in the GitHub repository.

git branch

‘git branch’ command is used to view branches that are present in the project. In the image below, * represents the current branch.

Image for post
Image for post
‘git branch’ is executed to view all the branches.

git branch <branch_name>

‘git branch <branch_name>’ command creates a new branch. In the example demonstrated below, the ‘git branch sign_up’ command is executed to create a ‘sign_up’ branch which will the required codes for the sign-up feature of an application.

Image for post
Image for post
‘git branch sign_up’ command is executed to create a branch named ‘sign_up’.

git checkout <branch_name>

‘git checkout <branch_name>’ command is used to switch the branches. In the image below, the ‘git checkout sign_up’ command is executed to switch to the ‘sign_up’ branch. Also, you can view your current branch from the name of the branch mentioned in parenthesis for instance, ‘(sign_up)’ as shown in the image below.

Image for post
Image for post
‘git checkout sign_up’ command executed to change the branch.

Now, we can use the ‘touch’ command for creating a file in the ‘sign_up’ branch. In the image below, ‘touch sign_up.html’ is used in order to create the file named ‘sign_up.html’.

Image for post
Image for post
‘touch sign_up.html’ being used to create a file named ‘sign_up.html’.

Now, you can push the file in a separate branch. Hence, for pushing files in separate branch you need to execute the following queries:

  • git add . : To take all files from your directory and push it to your remote repository. Here, ‘.’ indicates all files.
  • git commit -m <commit_details>: To make the first commit in the GitHub repository. Here, ‘-m’ stands for message
  • git push -u origin <branch_name>: To push/upload your files in your remote repository. git push -u origin <branch_name> sets the upstream of the current local branch which tracks the master branch of the remote repository origin. Hence, this command uploads only the changed files into your GitHub repository.
Image for post
Image for post
Pushing files into the ‘sign_up’ branch.

Finally, you can view the ‘sign_up’ branch in your repository as shown in the image below.

Image for post
Image for post
‘sign_up’ branch being presented in ‘RepositoryDemo’ repository.

Now, let’s switch into your ‘master’ branch by executing the command as ‘git checkout master’. Now, in your local repository, your ‘sign_up.html’ page has not been shown after you switch to your master branch. Therefore, until and unless you merge your ‘sign_up’ branch with your ‘master’ branch, other contents of other branches will not be presented in the ‘master’ branch. Hence, only after you reach a stable version of the feature of your application you will be merging contents of other branches with the ‘master’ branch. This allows individuals not only to organize their code but also keeps the ‘master’ branch clean and stable.

Image for post
Image for post
Only the ‘index.html’ file is shown after changing branches in the local repository.

git merge <branch_name>

Let us suppose that you have not reached a stable version for your sign-up feature of your application. Now, you are willing to merge your selected branch with the master branch. So, in order to merge with the ‘master’ branch at first, you are required to switch to your ‘master’ branch. Then, provide git command as ‘git merge <branch_name>’. For instance ‘git merge sign_up’ to merge your ‘sign_up’ branch with your ‘master’ branch.

Image for post
Image for post
After, executing the command as ‘git merge <branch_name>’ to merge the branches, the ‘sign_up’ file is shown in the master branch.

Now, files have not yet been pushed in the remote repository (master branch). Therefore, now you are required to push your files in your remote repository. Hence, for pushing files in the master branch you need to execute the following queries:

  • git add .
  • git commit -m <commit_message>
  • git push -u origin master
Image for post
Image for post
pushing the files into the ‘master’ branch.

Finally, you can now view your files in the ‘master’ branch. In the image below, ‘sign_up.html’ appears after the files are pushed into the ‘master’ branch.

Image for post
Image for post
All the files are now pushed into the remote repository.

git branch -d <branch_name>

Since the purpose of the ‘sign_up’ branch has now been fulfilled, let’s delete the ‘sign_up’ branch. ‘git branch -d <branch_name>’ is used to delete the branch. In the example illustrated in the image below, the ‘git branch -d sign_up’ command is used to delete the ‘sign_up’ branch. Then, the ‘git branch’ command is executed to view all the branches.

Image for post
Image for post
git branch -d sign_up being executed to delete a branch.

git push <remote> — delete <branch>

If you are willing to remove your branch even from your remote repository, you can use the command as ‘git push <remote> — delete <branch>’. For example, ‘git push origin — delete sign_up’ removes the ‘sign_up’ branch even from your remote repository.

Image for post
Image for post
git push origin — delete sign_up executed to delete the branch from remote repository.

After executing the ‘git push origin — delete sign_up’, you no longer can see your ‘sign_up’ branch in your remote repository.

Image for post
Image for post
Now, the ‘sign_up’ branch is removed from the remote repository.

git checkout -b <branch_name>

‘git checkout -b <branch_name>’ command can be used to create and switch the branch simultaneously. Here, ‘git checkout -b log_in’ command has been executed to create a branch called ‘log_in’ and checkout to the ‘log_in’ branch.

Image for post
Image for post
git checkout -b log_in being executed to create and switch to the ‘log_in’ branch simultaneously.

Written by

Cloud Computing | Data Science | Mobile Application Development | Artificial Intelligence | Python Programming | Soft Skills | Many more…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store