How to Delete a GitHub Branch (Local and Remote Guide)


XINSTALL BY CLICKING THE DOWNLOAD FILE
A message from our partner

For fixing Windows errors, we recommend Fortect:

Fortect will identify and deploy the correct fix for your Windows errors. Follow the 3 easy steps to get rid of Windows errors:

  • Download Fortect and install it on your PC
  • Launch the tool and Start scanning your PC for Windows errors
  • Right-click on Start Repair to deploy the right fix for each error encountered during the scan
Download Now Fortect has been downloaded by 0 readers this month, rated 4.6 on TrustPilot

Deleting a branch in GitHub is a common task when cleaning up merged or outdated code. This guide shows you how to remove both local and remote branches step by step.

How to Delete a GitHub Branch?

1. Delete a Local Branch

Follow these steps in your Git terminal:

  1. Open your terminal or command prompt.
  2. Navigate to your repository folder.
  3. Delete a merged branch by running git branch -d branch-name
  4. Force delete an unmerged branch by running git branch -D branch-name

This removes the branch only from your local machine.

For more basic Git operations, check out this guide on how to clone a repository in GitHub. If you’re interested in repository organization alongside branch cleanup, see this guide on how to create a folder in GitHub.

2. Delete a Remote Branch

To delete from GitHub’s remote repository:

  1. Make sure you pushed or merged changes if needed.
  2. Delete the branch remotely by running git push origin --delete branch-name
  3. Alternatively, use the following command to remove it: git push origin :branch-name

This ensures the branch is removed from GitHub so teammates cannot access it.

Need a quick backup of a single directory before removing the branch? Learn how to download a folder from GitHub and save only the files you need.

3. Delete a Branch in GitHub Web Interface

If you prefer using GitHub’s site:

  1. Go to your repository on GitHub.
  2. Click Branches in the top menu.
  3. Locate the branch you want to remove.
  4. Click the trash icon next to it.

The branch will be deleted immediately.

If you also want to share your work online, here’s a guide on how to host files on GitHub.

FAQs

How do I delete a branch in GitHub Desktop?

Go to the Branch menu, select the branch, and click Delete.

Can I recover a deleted branch?

Yes. If it had recent commits, you can restore it from the commit history.

Does deleting a branch delete commits?

No. Commits remain in the repository’s history even after branch deletion.

Can I delete the main branch in GitHub?

No. The default branch (main or master) cannot be deleted unless you change the default branch first.

Conclusion

Deleting branches in GitHub helps keep your repository clean and manageable. Use the command line or GitHub’s interface to remove both local and remote branches when they are no longer needed.

After removing outdated branches, it is equally important to understand how to merge branches in GitHub so you can keep your project streamlined and ensure changes flow back into the main branch smoothly.

To take your coding further, you might also want to explore using GitHub Copilot in Visual Studio.

More about the topics: Github

Readers help support Windows Report. We may get a commission if you buy through our links. Tooltip Icon

Read our disclosure page to find out how can you help Windows Report sustain the editorial team. Read more

User forum

0 messages