If you are working in a repository with a lot of activity, the number of branches created can add up quickly. There will come a time when you need to delete merged branches or branches that are no longer needed. I will guide you to do that.
Delete Branch (branch) using GitHub’s website
You can delete branches using GitHub’s website. However, you can only delete remote branches with this method — you cannot delete local branches from the GitHub website.
To get started, visit the GitHub website and log in to your account. Once logged in, select the branch repository you want to delete from the menu on the left.
Next, click “Branches” below the title menu.
A list of branches will appear. Find the branch you want to delete, then click the red trash can to the right of that branch.
The branch is now deleted. To map this change in your local repository, change to the respective directory, checkout the master branch, then run the git –pull command from the terminal.
Delete a local or remote branch with the command
You can delete both local and remote branches with the command. First, open terminal or cmd, go to GitHub repository’s directory (cd), then checkout master branch by running git checkout command.
There are two different commands you can run to delete a local branch. If it is already merged, run the command:
git branch -d <branch-name>
Or, to force delete a branch regardless of its current state, run the command:
git branch -D <branch-name>
Just replace with the actual name of your branch. For example, if my branch name is test-branch, then I would run the command:
git branch -d test-branch
The local branch is now deleted. If you want to delete a remote branch, run the command:
git push <remote-name> --delete <branch-name>
Replace with your branch name. Eg:
git push origin --delete test-branch
The remote branch is now deleted. If you’re deleting branches in the GitHub repository that are no longer active or needed, you don’t need to delete them one by one — you can delete the entire repository. Alternatively, you can also learn how to use git in its entirety here.