Git

How to Delete a Git Branch Locally?

One of the important features of GitHub is the branch that contains the copy of the project from a particular point. It helps manage the project work properly. When all project works are done and approved, then it is necessary to combine the works from the different branches to the main branch. After merging the project work, it is required to delete the unnecessary branches. The GitHub server contains the main files and folders for the user. The owner of the GitHub account can access the file and folders of the different repositories at any time and if any file is changed locally, then the owner can publish the updated content from the local system to the main server. The branches of the main server are called remote branches and the branches of the local system are called local branches. GitHub Desktop has used it in this tutorial to create, access, and update the repositories of the local drive and GitHub server. The branch of the repository can be deleted by executing commands in the terminal or by using the GitHub Desktop. These two ways of deleting any local git branch have been shown in this tutorial.

Prerequisites:

Install GitHub Desktop

GitHub Desktop helps the git user to perform the git-related tasks graphically. You can easily download the latest version installer of this application for Ubuntu from github.com. You have to install and configure this application after download to use it. You can also check the tutorial for installing GitHub Desktop on Ubuntu to know the installation process properly.

Create a Repository in the Local Drive

You have to create a local repository to check the commands used in this tutorial for deleting any branch locally.

Create Multiple Branches

Create multiple branches to delete any branch locally because if there is only one active branch can’t be deleted.

Delete Local Branch from the Terminal:

When the git user creates a branch in the local repository, then the branch is stored locally. The local branch can be deleted before or after publishing in the remote server. If the user deletes the branch without publishing the remote server, then it will not generate any effect in the remote branch. There are two options to delete the branch using the git command. The -d option is used to delete the branch that has been published in the remote branch. The -D option is used to delete the local branch forcefully that has not been published in the remote branch.

Open the terminal and go to the local repository location that contains the multiple branches. Run the following command to check the list of the existing branches of the current repository.

$ git branch

The following output shows that the repository contains three branches. These are main, master, and secondary.

Git checkout command is used to navigate between the branches and provide updated information about the particular branch if the branch is published in the remote server. Run the following command to switch into the branch named main and get the updated information of this branch.

$ git checkout main

The following output shows that the branch named main is active now and up to date with the main branch of the remote server. You can’t delete any active branch. So, if you run the delete command for deleting any active branch, then an error will be generated.

Run the following command to delete the local branch named main that is published in the remote server with the -d option.

$ git branch -d main

The following output shows that the main branch can’t be deleted because it is an active branch.

Run the following commands to delete the branch named master that is published in the remote server and retrieve the branch list to check the branch is deleted or not.

$ git branch -d master
$ git branch

The following output shows that the master branch has been deleted locally because it was not the active branch but was published in the remote server. The existing branch list has been checked by the second command that shows that the branch has been deleted properly.

You can delete any unpublished local branch by using the following command. There is no unpublished branch in the current repository. So, the output of this command is not shown.

$ git branch -D main

Delete Local Branch from the GitHub Desktop:

Many git users don’t like command-line tasks and prefer a graphical user interface to do git-related tasks. GitHub Desktop is implemented for those users. If you want to delete the local branch of any repository without typing the commands, then open the GitHub Desktop application and open the local repository to delete the particular branch. Here, a repository name Django is opened that contains two branches named, main and secondary. Click on the Branch list from the View menu to display all branches of the current repository. The following output shows that the main branch is the default branch.

Select the branch that you want to delete and click on the Branch menu that contains many sub-menu items to do different types of branch-related operations. You have to click on the Delete sub-menu item to delete the selected branch.

Conclusion:

Sometimes the git developer needs to delete unnecessary branches from the git repository. The branch can be deleted locally and remotely. The ways to delete any branch locally have been described in this tutorial. The branch can be deleted locally by using a command-line or GUI. The unpublished local branch can also be deleted forcefully using the -D option. Hopefully, the git user will be able to delete any branch from any local repository after reading this tutorial properly.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.