Git push accepts a source and destination refspec as part of the push operation, but I can not seem to push a local tag as a new remote branch in a single command. I am aware I could checkout the tag and then push it, but this should be possible as a single command.

What I've tried:

git push origin refs/tags/0.0.1:new_branch

What happens:

Counting objects: 1, done.
Writing objects: 100% (1/1), 156 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 1 (delta 0)
To '■■■■■■■■■■■■■■■■■■■■■■■■■■■■.com/test.git'
  * [new tag]         0.0.1 -> new_branch

I've also tried variations of remotes/origin/master instead of master, but this also creates a new tag instead of creating a remote branch based on the tag.

git push origin refs/tags/0.0.1^{commit}:refs/heads/new_branch

I've found the answer from another answer here. https://stackoverflow.com/a/4061542/6669458

| improve this answer | |
  • 1
    So, the trick is that you have to add "^{commit}" to the source so that it knows to look at the commit from that tag, and for the destination, you have to prefix it with "refs/heads/" so that it makes a branch instead of a tag. – Jacob Brown Nov 11 '19 at 15:33
  • Based on that post, git push origin +0.0.1~0:refs/heads/new_branch did what I wanted. – Afforess Dec 9 '19 at 23:22

You can always push the tag directly, it will be pushed detached of a branch.

git push origin 0.0.1

What will happen:

Counting objects: 1, done.
Delta compression using up to 8 threads.
Compressing objects: 100% 1/1), done.
Writing objects: 100% (1/1), 685 bytes | 0 bytes/s, done.
Total 1 (delta 1), reused 0 (delta 0)
To git@github.con:group-something/test.git
 * [new tag]         0.0.1 -> 0.0.1

If you want to use your ref syntax, it would be as follows:

git push origin refs/tags/0.0.1:refs/tags/0.0.1

The result is exactly the same.

In both cases you need to be aware that you are pushing a detached head. This means that anyone pulling or fetching will not get this tagged head. You need fetch with the --tags param. Otherwise only the tags in branches will be pulled. So only when you do:

git fetch --tags

You get the "detached" heads referenced by your tags.

remote: Counting objects: 6, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 6 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.
From git@github.con:group-something/test
 * [new tag]         0.0.1 -> 0.0.1
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.