
Module 5 Lesson 5: Deleting remote branches
Keep the server clean. Learn the commands for deleting branches on the remote server and how to 'prune' your local repo to remove stale references to deleted branches.
Module 5 Lesson 5: Deleting remote branches
You've finished your feature, merged it into main, and deleted your local branch. But on GitHub, the branch still exists! If you don't delete remote branches, your server will eventually become cluttered with hundreds of "dead" branches.
In this lesson, we learn how to cleanly remove branches from the remote server.
1. The Delete Command
To delete a branch from a remote repository, you use the git push command with a special flag:
# git push <remote> --delete <branch-name>
git push origin --delete feature-login
Common Mistake: Many users think git branch -d deletes the remote branch. It doesn't. You must push the deletion to the server.
2. Pruning Stale Branches
Even after you (or a teammate) delete a branch on the server, your local machine might still show a "Remote-Tracking" branch for it (e.g., origin/feature-login). This is called a stale reference.
To clean up these stale local references, use the prune flag with fetch:
git fetch --prune
This command checks the server, sees which branches are gone, and deletes the corresponding local origin/... pointers.
3. The "Force" Delete
Just as with local branches, Git might prevent you from deleting a remote branch if it thinks there is work that hasn't been merged yet. However, since you are pushing a "delete" command, that logic usually doesn't apply to remotes in the same way. The command above is usually enough.
graph LR
Local["Local Repo"] -- "git push origin --delete" --> Remote["Remote Server"]
Remote -- "Branch erased" --> Remote
Remote -- "Local still has origin/branch_name" --> Local
Local -- "git fetch --prune" --> CleanLocal["References Cleaned"]
Module 5 Practice Exercises
Let's practice the full remote lifecycle:
- Setup: Create a new branch
remote-test. - Push: Push it to your remote with upstream tracking (
-u). - Verification: Go to the GitHub website and confirm the branch is visible.
- Local Cleanup: Switch to
mainand delete theremote-testbranch locally (git branch -d). - Remote Cleanup: Delete the branch from the server using
git push origin --delete. - The Prune: Run
git branch -a(to see all branches including remotes). Doesorigin/remote-teststill show up? - Run
git fetch --prune. Now rungit branch -aagain. Is it gone?
Checkpoint: If you can successfully create, push, delete, and prune a branch, you have mastered the basics of team collaboration in Git!
Summary
In this lesson, we established:
git push <remote> --delete <branch>removes the branch from the server.git fetch --prunecleans up your local "remembrances" of deleted remote branches.- Keeping both local and remote environments clean is a hallmark of a professional developer.
Module Complete! You are now ready to work with teams and remote servers like a pro.
Next Module: We’ll move into the high-performance features of Git. Welcome to Module 6: Advanced Git Features, where we'll master Stashing, Rebasing, and Cherry-picking.