5

当我执行branch -a

$ git branch -a
* master
 remotes/origin/HEAD -> origin/master
 remotes/origin/hello
 remotes/origin/master

然后我删除分支:

$ git branch -r -D origin/hello
Deleted remote branch origin/hello (was c0cbfd0).

现在我明白了:

$ git branch -a
* master
 remotes/origin/HEAD -> origin/master
 remotes/origin/master

分支“hello”已被删除。但是当我获取时:

$ 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

我很混乱。
我认为它已被删除,但它仍然存在。

4

4 回答 4

6

您需要使用以下命令将其从遥控器中删除:

git push origin --delete hello

当您运行时git branch -rd origin/hello,您只会删除本地分支。上面的代码将其从原始存储库中删除。

于 2012-09-05T06:27:50.310 回答
3

删除远程分支,请使用

git push origin :remotebranch

其他一切仅在本地存储库上运行。在最新版本的 git 中,您还可以

git push origin --delete remotebranch

根据文档--delete这意味着“与在所有引用前加一个冒号的前缀”相同。

如果您想知道 的含义:,它遵循 . 的标准语法push。通常,你会写

git push origin localbranch:remotebranch

但是在这里,你localbranch用“nothing”替换,有效地删除了远程分支。

于 2012-09-05T06:32:56.793 回答
1

请注意, git branch 只允许删除本地引用。

 git branch -r -D origin/hello

这只删除了指向远程跟踪分支的本地指针,但对远程 repo 内容本身没有影响。
正如其他答案中提到的那样,只有 才git push origin :hello会这样做。

另外,这不会改变配置branch.hello.fetch:它仍然引用 origin/hello,这就是为什么下一次 fetch 将在本地 repo 中重新创建远程跟踪分支的原因。

于 2012-09-05T07:32:25.460 回答
0
git push origin --delete somebranch

是您删除远程分支的方式。如果您仍在使用旧版本的 Git,则可能需要使用旧语法:

git push origin :somebranch

翻译为“将任何内容推入原点指向的遥控器上的某个分支”。该命令的格式为“git push (which remote repo) (what local reference):(which remote reference)。省略 (what reference) 被解释为“什么都不放”到 (which remote reference) 中,有效地删除它。较新的语法更直观。

于 2012-09-05T06:47:12.797 回答