A2oz

How Do You Delete a Branch in GitHub?

Published in Git and Version Control 2 mins read

You can delete a branch in GitHub directly from the web interface or using the command line. Here's how:

Deleting a Branch from the GitHub Website

  1. Navigate to the repository: Go to the GitHub repository where the branch you want to delete is located.
  2. Select the "Branches" tab: Click on the "Branches" tab in the repository's navigation bar.
  3. Locate the branch: Find the branch you want to delete in the list of branches.
  4. Click the "Delete" button: Next to the branch name, you'll find a "Delete" button. Click on it.
  5. Confirm the deletion: GitHub will prompt you to confirm the deletion. Type the branch name and click "Delete branch".

Deleting a Branch Using the Command Line

  1. Open your terminal: Open your terminal or command prompt.
  2. Navigate to the repository: Use the cd command to navigate to the local directory of your repository.
  3. Delete the branch: Run the following command, replacing branch-name with the actual name of the branch:
     git branch -d branch-name
  4. Push the changes: If you want to remove the branch from the remote repository, run the following command:
     git push origin :branch-name

Remember:

  • You can only delete branches that have been merged into the main branch or are not being used anymore.
  • Deleting a branch removes it from both your local repository and the remote repository.

Practical Insights

  • Deleting a branch can be risky if it's actively being used. Make sure you've merged any necessary changes into the main branch before deleting it.
  • Use the git branch -D command to force delete a branch. This is useful if you need to delete a branch that has unmerged changes.

Related Articles