217

I want to force push, for example, my tag 1.0.0 to my remote master branch.

I'm now doing the following:

git push production +1.0.0:master

I want to force the push, because all I care about is that the code inside the 1.0.0 tag is pushed to the master branch on the remote repository.

What am I doing wrong?

Update #1

When I SSH into my server where my Git repository is and execute git branch -l, I don't see the master branch listed either.

Update #2

After running git tag -l from inside the remote Git repository, I see that master is listed, meaning that when I ran the following:

git push production 1.0.0:master

It actually pushed the tag and created a tag named master rather than a new branch.

I want to basically push the contents of the tag 1.0.0 into the master branch of the remote Git repository.

7
  • Can you clarify what "not working" means? Does Git give an specific error, or does it have a null effect?
    – vcsjones
    Oct 31, 2010 at 1:51
  • I'm sorry. Yes, so basically when I SSH into my server, into the git repository, and run git branch -l to list the branches, I only see my other branch. However, the git push production +1.0.0:master did push, an when I re-push it says Everything up-to-date, but I don't see the master branch on the remote server. Oct 31, 2010 at 1:55
  • 5
    You should change the accepted answer. The second answer is much simpler than the one which is marked as accepted.
    – Pedro Rolo
    Sep 14, 2012 at 15:55
  • Sorry for the late response. I agree and have now changed the accepted answer. Dec 22, 2012 at 12:27
  • 1
    @MichaelvanRooijen I don't understand how the accepted answer that you chose actually solves this problem. It doesn't overwrite a branch with a tag, it just pushes your tags to the remote.
    – user456814
    Apr 27, 2014 at 20:08

4 Answers 4

469
git push --tags production
6
  • 4
    If the tag already exists on the remote you will need to delete the remote tag first with git push production :1.0.0. Jan 23, 2013 at 14:38
  • 1
    If by any occasion you'll have branch with same name: '1.0.0' this push will fail so better use: git push production :refs/tags/1.0.0 to delete tag only
    – Vladimir
    Apr 19, 2013 at 20:08
  • 1
    @Nerian: I think it just pushes tags
    – bstpierre
    Jul 20, 2013 at 17:09
  • 7
    How does this actually solve the original poster's problem about overwriting a branch with a tag by force-pushing it? This just pushes all of your tags to a remote, it won't overwrite any branches.
    – user456814
    Apr 27, 2014 at 19:53
  • 1
    Isn't the question asking how to push one tag? This command does a whole lot more than that. Sep 11, 2014 at 6:27
64

It is probably failing because 1.0.0 is an annotated tag. Perhaps you saw the following error message:

error: Trying to write non-commit object to branch refs/heads/master

Annotated tags have their own distinct type of object that points to the tagged commit object. Branches can not usefully point to tag objects, only commit objects. You need to “peel” the annotated tag back to commit object and push that instead.

git push production +1.0.0^{commit}:master
git push production +1.0.0~0:master          # shorthand

There is another syntax that would also work in this case, but it means something slightly different if the tag object points to something other than a commit (or a tag object that points to (a tag object that points to a …) a commit).

git push production +1.0.0^{}:master

These tag peeling syntaxes are described in git-rev-parse(1) under Specifying Revisions.

8
  • 1
    This solved the problem! However the master branch needs to exist already. This isn't an issue on my end however. Thanks a lot for your help! Oct 31, 2010 at 2:42
  • 2
    @Michael: Ahh. Yes, if master does not exist (as a branch or a tag), then git push rep +tag:master will create a tag named master instead of a branch. git push rep +tag~0:master (again, when master does not exist as a branch or a tag) will fail with “error: unable to push to unqualified destination”. The command that would have done what you wanted (before any master branch/tag existed) is git push rep +tag~0:refs/heads/master (refs/heads/ is the namespace under which branches are stored). Oct 31, 2010 at 3:13
  • GREAT! That will help me out amazingly well. Very convenient! Thanks a lot for posting that information as well. Oct 31, 2010 at 9:44
  • 4
    @brad: The ~{commit} syntax is literal (i.e. always those nine characters); the word commit is not a placeholder here. Aug 25, 2011 at 20:41
  • 1
    ah ok! sorry, I was thinking you meant to put in the particular commit, makes more sense now.
    – brad
    Aug 29, 2011 at 14:45
61

I create the tag like this and then I push it to GitHub:

git tag -a v1.1 -m "Version 1.1 is waiting for review"
git push --tags

Counting objects: 1, done.
Writing objects: 100% (1/1), 180 bytes, done.
Total 1 (delta 0), reused 0 (delta 0)
To [email protected]:neoneye/triangle_draw.git
 * [new tag]         v1.1 -> v1.1
2
  • 4
    it push all your tags
    – Gelldur
    Oct 1, 2013 at 18:55
  • 3
    Note that this won't actually solve the original poster's problem about overwriting a branch with a tag, it will just push your tags to a remote, without affecting branches.
    – user456814
    Apr 27, 2014 at 19:54
10

For pushing a single tag: git push <reponame> <tagname>

For instance, git push production 1.0.0. Tags are not bound to branches, they are bound to commits.

When you want to have the tag's content in the master branch, do that locally on your machine. I would assume that you continued developing in your local master branch. Then just a git push origin master should suffice.

1
  • 7
    Note that this won't actually solve the original poster's problem about overwriting a branch with a tag, it will just push your tags to a remote, without affecting branches.
    – user456814
    Apr 27, 2014 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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