Why does Git use a colon (:<branch>) to delete remote branches?

Git

Git Problem Overview


Why does Git use

git push <remote> :<branch>

as in

git push origin :featureA

to delete the branch featureA from the remote server?

I am interested in why the colon was used as the delete flag.

It's so different from git branch -d <localbranch>.

Why don't we do something like

git branch -d --remote origin <branchname>

or is there a deeper meaning behind the colon symbol that I didn't know?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionscalopusView Question on Stackoverflow
Solution 1 - GitmanojldsView Answer on Stackoverflow
Solution 2 - GitRyan StewartView Answer on Stackoverflow