Deleting a remote branch

*爱你&永不变心* 提交于 2019-12-18 05:03:13

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!