Git: Push a tag to a new remote branch in 1 command

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

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

I've found the answer from another answer here.

3

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like