Add a tag in remote repository without adding the repo on local

You can create a remote tag having no local tags at all with

git push origin HEAD:refs/tags/foo

You can remove the same tag with

git push origin :refs/tags/foo

Here's an explanation.

Take the command git push. Without being too strict, the general syntax could be interpreted as

git push where what:onto

where is the name of the remote repository you want to push to.

what a reference (using one of the several kinds offered by git) to a commit of your local repository. It can be a SHA1, a branch name, a tag name or other.

onto in the name you want the remote will use to reference the thing you are pushing.

For example

git push origin master:master

is pushing to origin the commit (and all the other previous commits, if the remote does not have them) pointed by master, asking the remote repository to call it master, that is to save in its master branch the exact same SHA1 recorded in your local master branch (this is not exactly true, but accept this example to understand the principle).

Now, as a matter of facts, branches and tags are just ordinary files storing the SHA1 of a commit. You could see them as sort pointer variables, with a reference to some commit in the repository. Branches and tags are stored in .git/refs/heads and .git/refs/tags

Try with

cat .git/refs/heads/master

So, the previous could have been written

git push origin refs/heads/master:refs/heads/master

If you want to create a tag foo in the remote repository pointing to the same commit that is referenced by your master branch, you could run

git push origin master:refs/tags/foo

If you want to create a tag on the remote repository referencing exactly the same commit you are in at the moment, use the special branch HEAD, which is a pointer to your current position

For example, try with

git checkout master
cat .git/refs/heads/master
cat .git/HEAD

It should give twice the same value, confirming that master and HEAD reference the same commit, that is, you are on master

So, in order to create a remote tag referencing your local current commit use

git push origin HEAD:/refs/tags/a_tag_name

It is somehow like you are asking the remote origin to write in its file refs/tags/a_tag_name the value of the SHA1 contained in your local HEAD. This creates the tag in the remote repository.

If you push a null you will delete the tag

git push origin :/refs/tags/a_tag_name

That's all

Tags:

Git