3

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.

2 Answers 2

2
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

3
  • 2
    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. Nov 11, 2019 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, 2019 at 23:22
  • Worth noting here that the "^{commit}" or "~0" bit is only required for annotated tags; for lightweight tags on the other hand the above works without this extra bit, eg git push origin 0.0.1:refs/heads/new_branch
    – Tao
    Aug 27, 2020 at 9:34
0

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 [email protected]: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 [email protected]:group-something/test
 * [new tag]         0.0.1 -> 0.0.1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .