问题
When I perform branch -a:
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/hello
remotes/origin/master
And then I remove the branch:
$ git branch -r -D origin/hello
Deleted remote branch origin/hello (was c0cbfd0).
Now I see:
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
The branch "hello" has been removed. But when I fetch:
$ git fetch
From localhost:project
* [new hello] hello -> origin/hello
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/hello
remotes/origin/master
I'm so confused.
I think it has been removed, but it is still there.
回答1:
You need to remove it from the remote with the following command:
git push origin --delete hello
When you are running git branch -rd origin/hello you are deleting your local branch only. The code above removes it from the origin repo.
回答2:
To delete a remote branch, use
git push origin :remotebranch
Everything else operates on the local repository only. In more recent versions of git, you can also
git push origin --delete remotebranch
As per the documentation, --delete means the same "as prefixing all refs with a colon".
If you are wondering about meaning of the :, it follows the standard syntax for push. Usually, you would write
git push origin localbranch:remotebranch
but here, you replace localbranch with "nothing", effectively deleting the remote branch.
回答3:
Note that git branch only allows for deleting local references.
git branch -r -D origin/hello
That only delete the local pointer to a remote tracking branch, but that has no influence on the remote repo content itself.
Only the git push origin :hello, as mentioned in the other answers, would do that.
Plus, that doesn't change the config branch.hello.fetch: it still references origin/hello, which is why the next fetch will re-create the remote tracking branch in your local repo.
回答4:
git push origin --delete somebranch
is the way you delete a remote branch. If you are still on an old version of Git, you may need to use the old syntax:
git push origin :somebranch
which translates to "push nothing into somebranch on the remote pointed to by origin". The command is of the form "git push (which remote repo) (what local reference):(which remote reference). Omitting (what reference) is interpreted as "put nothing" into (which remote reference), effectively deleting it. The newer syntax is much more intuitive.
来源:https://stackoverflow.com/questions/12275542/deleting-a-remote-branch