This post will discuss how to delete remote-tracking branches in git.

1. git-push

The git-push command is usually used to push local changes to a remote repository but can be used to delete remote branches as well.

We can do this by using git push with the -d option, an alias for --delete. This deletes the specified branch from the remote repository. The full command is:

git push <repository> [-d | --delete] <branchname>

To get the list of all remote branches, you can use the git-branch command with the -r option, an alias for --remotes. You might want to synchronize first with the remote server using git fetch.

git fetch
git branch (-r | --remotes)
git push origin --delete vlang

This is demonstrated below, where a branch vlang is deleted from the remote server origin.

Delete remote branch

 
Alternatively, you can prefix all refs with a colon.

git push <repository> :<branchname>

The following command will delete a ref that matches nim in the origin repository.

Delete a remote branch

2. git-branch

You can also delete the remote branches with the git-branch command, using the -r option with the -d option.

git branch -d -r <branchname>

This is demonstrated below:

remove remote branch using git-branch

 
Note that it only makes sense to delete remote-tracking branches if they no longer exist in the remote repository or if git fetch is configured not to fetch them. You can use the prune subcommand of git-remote for cleaning obsolete remote-tracking branches.

git remote prune origin

Alternatively, you can use the get-fetch command with the --prune option to remove any remote-tracking references that no longer exist on the remote.

git fetch --all --prune

That’s all about deleting remote-tracking branches in Git.