Often, you'll need to delete a branch in a remote repo after it has served it purpose.
To delete a branch in a remote repository, you simply tell Git to remove the reference to that branch from the remote. This does not delete the branch from your local repository — it only removes it from the remote, so others won’t see it anymore. This is useful for cleaning up clutter in the remote repo e.g., delete old or merged branches that are no longer needed on the remote.
1 Fork the samplerepo-books to your GitHub account. When doing so, un-tick the Copy the master branch only
option.
After forking, go to the fork and ensure all three branches are in there.
2 Clone the fork to your computer.
3 Create a local copy of the fantasy
branch in your clone.
Follow instructions in Lesson: Pushing Branches to a Remote.
4 Delete the remote branch fantasy
.
You can use the git push <remote> --delete <branch>
command to delete a branch in a remote. This is like pushing changes in a branch to a remote, except we request the branch to be deleted instead by adding the --delete
switch.
git push origin --delete fantasy
Locate the remote branch under REMOTES
→ origin
, right-click on the branch name, and choose Delete...
:

5 Verify the branch was deleted from the remote, by going to the fork on GitHub and checking the branches page https://github.com/{YOUR_USERNAME}/samplerepo-books/branches
e.g., https://github.com/johndoe/samplerepo-books/branches
.
Also verify the local copy has not been deleted.
6 Restore the remote branch from the local copy.
Push the local branch to the remote, while enabling the tracking option (as if pushing the branch to the remote for the first time), as covered in Lesson: Pushing Branches to a Remote.
In the above steps, we first created a local copy of the branch before deleting it in the remote repo. Doing so is optional. You can delete a remote branch without ever checking it out locally — you just need to know its name on the remote. Deleting the remote branch directly without creating a local copy is recommended if you simply want to clean up a remote branch you no longer need.