0
0
Gitdevops~5 mins

Deleting remote branches in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes branches on a remote repository are no longer needed and clutter the project. Deleting remote branches helps keep the project clean and organized by removing branches that are finished or obsolete.
When a feature branch has been merged and is no longer needed on the remote repository
When a remote branch was created by mistake and should be removed
When cleaning up stale branches that are not active anymore
When you want to reduce clutter in the list of remote branches
When a branch was pushed for testing but should not remain on the remote
Commands
This command deletes the remote branch named 'feature/login' from the 'origin' remote repository. It tells Git to remove that branch from the remote server.
Terminal
git push origin --delete feature/login
Expected OutputExpected
To https://github.com/example/repo.git - [deleted] feature/login
--delete - Specifies that the branch should be deleted on the remote
This command updates your local list of remote branches and removes references to branches that no longer exist on the remote. It cleans up your local view.
Terminal
git fetch --prune
Expected OutputExpected
From https://github.com/example/repo - [deleted] origin/feature/login
--prune - Removes local references to deleted remote branches
This command lists all remote branches your local Git knows about. After pruning, the deleted branch should no longer appear here.
Terminal
git branch -r
Expected OutputExpected
origin/main origin/develop origin/feature/payment
Key Concept

If you remember nothing else from this pattern, remember: use 'git push origin --delete branch-name' to remove a branch from the remote repository.

Common Mistakes
Trying to delete a remote branch using 'git branch -d branch-name'
'git branch -d' only deletes local branches, not remote ones, so the remote branch remains untouched.
Use 'git push origin --delete branch-name' to delete the branch from the remote repository.
Not running 'git fetch --prune' after deleting a remote branch
Your local Git still shows the deleted branch in the remote branch list, causing confusion.
Run 'git fetch --prune' to update and clean your local remote branch references.
Summary
Use 'git push origin --delete branch-name' to delete a branch from the remote repository.
Run 'git fetch --prune' to update your local remote branch list and remove deleted branches.
Check remote branches with 'git branch -r' to confirm the branch is deleted.