Module 5 Lesson 5: Deleting remote branches
·DevOps

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:

  1. Setup: Create a new branch remote-test.
  2. Push: Push it to your remote with upstream tracking (-u).
  3. Verification: Go to the GitHub website and confirm the branch is visible.
  4. Local Cleanup: Switch to main and delete the remote-test branch locally (git branch -d).
  5. Remote Cleanup: Delete the branch from the server using git push origin --delete.
  6. The Prune: Run git branch -a (to see all branches including remotes). Does origin/remote-test still show up?
  7. Run git fetch --prune. Now run git branch -a again. 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 --prune cleans 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.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn