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

Git 为什么使用

git push <remote> :<branch>

就是

git push origin :featureA

从远程服务器中删除分支 featureA

我感兴趣的是为什么使用冒号作为删除标志。

git branch -d <localbranch>完全不同。

我们为什么不做点什么呢

git branch -d --remote origin <branchname>

还是冒号背后有我不知道的深层含义?

21619 次浏览

The colon isn't a "delete flag". Note that git push and git pull both 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 :foo has an empty source and essentially says "push nothing to branch foo of origin", or, in other words, "make branch foo on origin not exist".

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

The refspec format is

<+><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.

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.