2

I want to delete a remote branch. How can I do git push origin --delete my-branch in magit, to delete a remote branch?

1 Answer 1

2

In Magit the command for deleting a remote branch is the same as for deleting a local branch, and its key binding is b k.

So to delete my-branch on origin you would type b k origin/my-branch RET.


Deleting the remote branch origin/feature involves two steps:

  1. Actually deleting the branch on the remote repository.
  2. Updating the cache in the local repository, which keeps track of the state of the remote repository.

Deleting a remote branch does not delete the corresponding local branch, feature. I.e., the local ref refs/heads/feature is not being deleted. What is being deleted is the "remote-tracking branch", represented in the local repository by the ref refs/remotes/origin/feature, which is a reference to the state of the remote branch.

There likely is a relationship between the branch feature and the remote-tracking branch origin/feature, but deleting one does not cause the other to be deleted. However refs/remotes/origin/feature is how the local repository keeps track of "feature on origin", so deleting the former should go in hand with deleting the latter.

When you delete a remote branch In Magit, then it prompts with Delete origin/feature on the remote (not just locally)?. The "not just locally" was added because a user might expect that only refs/remotes/origin/feature is deleted but not "feature on origin".

Deleting only refs/remotes/origin/feature only makes sense in very rare situations. Doing so would essentially make the local cache of the remote state incorrect and later fetching from the remote again would fix that defect, i.e., refs/remotes/origin/feature would be recreated.

Yet, some users expected, that that is what "deleting a remote branch" would do, so the warning was added. Unfortunately that warning led other users to believe that deleting a remote branch also causes the local branch to be deleted.

5
  • b k only removes the local one. The pushed remote branch still exists on Github
    – Student
    Nov 29, 2020 at 12:39
  • b k first tries to delete the remote branch using git push origin --delete my-branch and if and only if that fails (e.g. because you don't have write permissions), then it deletes the respective local ref only.
    – tarsius
    Nov 29, 2020 at 17:05
  • 1
    @Student Errr... Just so you know, tarsius is the chief maintainer of magit. :-)
    – TerryTsao
    Nov 30, 2020 at 3:03
  • That is scary behavior, now I know why part of the prompt for removing a remote branch says "....(not just locally)". I love magit, but that could be disastrous (deleting a local branch when you meant only to delete a remote), no? Love magit by the way, thank you!!
    – Joe
    Jun 28, 2022 at 2:49
  • @Joe The local branch is not deleted, see the updates answer.
    – tarsius
    Aug 12, 2022 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.