Learn step-by-step how to delete a GitHub branch locally and remotely, manage permissions, and clean up pull requests effectively.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 0: Ensure you have the necessary permissions
Before you delete any branch on GitHub, confirm that you have write or admin access to the repository. Without the correct permissions, you will not see the delete option.
Step 1: Identify the branch you want to delete
First, determine which branch needs to be removed. You can do this either on the GitHub website or in your local repository.
On the GitHub website:
In your local repository:
git branch
This command lists all local branches and highlights the current one.
Step 2: Delete the branch locally (optional)
If you have the branch checked out or already fetched locally and want to remove it from your machine, run:
git branch -d branch-name
If Git prevents deletion due to unmerged changes, force deletion with:
git branch -D branch-name
Note: Deleting a local branch does not affect the remote branch on GitHub.
Step 3: Delete the branch on the GitHub website
To remove the branch directly in the web interface:
Step 4: Delete the branch using Git command line
If you prefer the command line or need to automate the process, you can delete a remote branch with:
git push origin --delete branch-name
After running this, you should see output similar to:
To github.com:username/repo.git
- [deleted] branch-name
Step 5: Verify that the branch has been deleted
Make sure the branch no longer exists both locally and remotely.
Check on GitHub website:
Check from your local machine:
git fetch --prune
git branch -r
Step 6: Clean up any references in pull requests
If you opened a Pull Request from the deleted branch, you may wish to close it since the source branch is gone.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.