3

I have a local branch, say hotfix. I put it up on GitHub using

$ git push -u origin hotfix

Then after merging the branch, I want to delete it. So I type

$ git branch -d hotfix

which deletes the branch locally. However, the remote branch is still on GitHub. Of course when I look at my remote branches,

$ git branch -r
  origin/HEAD -> origin/master
  origin/hotfix
  origin/master

hotfix is still there. So then I try

$ git branch -r -d origin/hotfix
$ git branch -r
  origin/HEAD -> origin/master
  origin/master

and hotfix is gone, as expected.

But then I go to GitHub and look at my branches, and hotfix is still there! How can I delete the remote hotfix from GitHub via the command line, without having to navigate to GitHub in my browser and delete it using the Website UI?

chharvey
  • 8,580
  • 9
  • 56
  • 95

1 Answers1

6

You need to push that deletion:

git push origin --delete hotfix

(any git branch command would only have an effect on your local repo, not the GitHub remote one)

See more at "How to delete a Git branch both locally and remotely?".

A git branch -d -r only deletes a remote tracking branch, that is branches existing in the remotes/origin namespace of your local repo. GitHub (the actual remote repo) is never notified.

What you have "successfully" deleted is the "remote tracking branch" which is in your local repo and is meant to record the last SHA1 that you fetched from the upstream repo for that branch: that what a "remote tracking branch" is.

The correct sequence is:

git push origin --delete hotfix
git remote update --prune origin

That will delete the hotfix branch on the GitHub repo, and then delete any obsolete remote tracking branch in your repo.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • How is this command different from `git branch --remote --delete origin/hotfix`? – chharvey Jun 14 '14 at 05:17
  • @chharvey `git branch` is for modifying branches in your local repo: GitHub is never notified. See my edited answer. – VonC Jun 14 '14 at 05:21
  • @chharvey you have "successfully" deleted a remote tracking branch (which is in your repo in order to record the last SHA1 f that branch fetched on the remote repo), not the branch on the actual remote repo. – VonC Jun 14 '14 at 05:23
  • @chharvey conclusion: a remote (tracking) branch in your local repo isn't the same as a branch in your remote repo. – VonC Jun 14 '14 at 05:31
  • Got it. One last question: when I use `git push origin -d hotfix`, should I be on branch `hotfix` or on `master`? – chharvey Jun 14 '14 at 05:31
  • @chharvey on any branch, it doesn't matter: it will influence only the upstream repo (GitHub). Your local repo won't notice it until you prune your "remotes" (that is the fetched references you have of your remote). – VonC Jun 14 '14 at 05:33