Edit: The below description is of course still accurate, but it should be noted that Magit now falls back to deleting just the local remote-tracking ref if the respective branch does not actually exist on the remote anymore.
Pulling out the relevant information from the question comments:
Magit does not provide a direct equivalent of git branch -D -r <remote>/<branch>
.
The --prune
flag from the fetch popup (by default, f-p)
can be used instead to delete all stale remote-tracking for a
particular remote.
I believe that Magit doesn't provide a command for git branch -D -r
because it is the assumed that it would be rare for a user to want to
delete a single stale remote-tracking branch but not prune other stale
remote-tracking branches for that remote.
If the branch does still exist on a remote, k will run
git push <remote> :<branch>
, deleting the branch on the remote and
cleaning up the local remote-tracking branch. So, provided that
the branch hasn't been deleted from the remote in some other way
(e.g., through GitHub's web interface or by running git push <remote> :<branch>
from another machine), this command is all you need to run (at least
until you get on another machine and want to prune the remote-tracking
branches there).
Unlike git push <remote> :<branch>
, git branch -D -r <remote>/<branch>
is not sufficient to delete the branch on the remote. This is
described in man git-branch
:
Use -r together with -d to delete remote-tracking branches. Note, that it only makes
sense to delete remote-tracking branches if they no longer exist in the remote
repository or if git fetch was configured not to fetch them again. See also the prune
subcommand of git-remote(1) for a way to clean up all obsolete
remote-tracking branches.
The example below shows what the above quote is talking about. If you
just use git branch -D -r
, the local remote-tracking branch will
deleted just to be recreated on the next fetch.
set -x
# Repo setup
mkdir remote-repo
cd remote-repo
git init --bare
cd ..
mkdir local-repo
cd local-repo
git init
echo text > some-file
git add some-file
git commit -m"Add some file"
git checkout -b feat master
echo text > feat-file
git add feat-file
git commit -m"Add feature file"
git remote add origin ../remote-repo
git push origin feat master
# branch -D -r test
git branch -D -r origin/feat
ls .git/refs/remotes/origin/
git fetch origin
ls .git/refs/remotes/origin/
But a git push origin :feat
(or, in Magit, k and then
selecting origin/feat
) will get rid of the branch in both places.
git push origin :feat
ls .git/refs/remotes/origin/
git fetch origin
ls .git/refs/remotes/origin/
git branch -D -r
] very much is sufficient [to delete remote branches]" - not according to the manual: "it only makes sense to delete remote-tracking branches if they no longer exist in the remote repository"