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.
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.
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.
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
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.
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.