为什么 Git 使用冒号 (:<branch>) 来删除远程分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7303687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 05:56:44  来源:igfitidea点击:

Why Git use the colon (:<branch>) to delete remote branch

git

提问by scalopus

I am thinking about why the Git command use

我在想为什么 Git 命令使用

git push <remote> :<branch>like git push origin :featureAto delete featureA branch in the remote server. I am interest that why make the colon as the delete flag. It is so difference from

git push <remote> :<branch>喜欢 git push origin :featureA删除远程服务器中的 featureA 分支。我很感兴趣为什么将冒号作为删除标志。它与如此不同

git branch -d <localbranch>

git branch -d <localbranch>

Why don't make something like

为什么不做类似的事情

git branch -d --remote origin <branchname>

git branch -d --remote origin <branchname>

or there are deep meaning of colon symbol that I never know before?

或者有我以前不知道的冒号符号的深层含义?

回答by manojlds

It is not the meaning of the :per se, but what is present, or rather absent before it.

它不是:本身的意义,而是在它之前存在的,或者更确切地说是不存在的。

The refspec format is

refspec 格式是

<+><source>:<destination>

(optional + for non-fast forward)

(可选 + 用于非快进)

So when you do something like git push origin :featureA, you are specifying an empty source ref and basically making the destination "empty" or deleting it.

因此,当您执行类似操作时git push origin :featureA,您是在指定一个空的源引用,并且基本上将目标设置为“空”或将其删除。

PS: Note that the refspec of :or nothing doesn't mean push nothing to nothing however. It makes git to push "matching" branches: for every branch that exists on the local side, the remote side is updated if a branch of the same name already exists on the remote side.

PS:请注意, refspec of :or nothing 并不意味着什么都没有。它使 git 推送“匹配”分支:对于本地端存在的每个分支,如果远程端已经存在同名分支,则更新远程端。

回答by Ryan Stewart

The colon isn't a "delete flag". Note that git pushand git pullboth accept zero or more refspecs as their final argument(s). Now read about refspecs. A colon separates source from destination in a refspec. The command git push origin :foohas an empty source and essentially says "push nothingto branch foo of origin", or, in other words, "make branch foo on origin not exist".

冒号不是“删除标志”。请注意,git pushgit pull都接受零个或多个 refspecs 作为它们的最终参数。现在阅读 refspecs。在 refspec 中,冒号将源与目标分开。该命令git push origin :foo有一个空的源代码,基本上是说“向分支 foo推送任何内容”,或者换句话说,“使分支 foo 不存在”。