34

I'm trying to create a branch from a remote tag, but it seems there's no way to do it. When I try

git checkout -b test origin/deploy

where origin is the remote and deploy is the tag I want to check out, but I get

fatal: git checkout: updating paths is incompatible with switching branches.
Did you intend to checkout 'origin/deploy' which can not be resolved as commit?

UPDATE: I've just discovered that

git fetch --all -t

was not working properly for me. While it downloads all branches, it does not download all tags, so when I checked out deploy it was and old tag. Now I execute

git fetch --all && git fetch -t

This way when I create a new branch based on a tag

git checkout -b test deploy

the new branch is up to date with the last deploy.

Sergi
  • 2,872
  • 2
  • 25
  • 24

4 Answers4

30

I'm not sure you can do this directly. You're probably stuck with doing a fetch and then a checkout:

git fetch origin
git checkout -b test tag-name

By the way, I wouldn't recommend using a tag name like "deploy".

Joost Diepenmaat
  • 17,633
  • 3
  • 44
  • 53
  • Well, now I'm always doing "git fetch --all -t" before the checkout, but in some cases the branches created from the tag seem to be pointing to an old deploy instead of the last one. BTW, why wouldn't you use the name "deploy"? – Sergi Sep 27 '11 at 17:36
  • 1
    @Sergi, tags are intended to remain fixed, but a name like "deploy" implies you will be changing it frequently. It's better to use a branch to label a line of development that changes over time, and have your tags be specific versions, like "1.0". – Karl Bielefeldt Sep 27 '11 at 17:51
  • @Joost, oh, don't worry then. I always create two tags when I deploy, one with the version name, and another one called deploy, which is overwritten each time I make a new deploy. This way the rest of developers could branch from the last good point on the deployment branch. Any idea why sometimes do I get branches pointing to and old deploy? Could it be that the "git fetch --all -t" is not working as expected? – Sergi Sep 27 '11 at 17:58
  • 1
    I don't know how duplicate tags are resolved. It's implied tags are supposed to be fixed and unique. I found that using a "deploy" branch with tags for specific releases works very well; that way you have something that always points to the current release (the HEAD of that branch) and fixed references to past releases (tags with version number or dates). – Joost Diepenmaat Sep 27 '11 at 18:02
  • @Sergi, you can do as you like. But the Git documentation is very explicit about the fact that tags shouldn't be changed or reused. From the git-tag documentation: "If somebody got a release tag from you, you cannot just change the tag for them by updating your own one. This is a big security issue, in that people MUST be able to trust their tag-names. If you really want to do the insane thing, you need to just fess up to it, and tell people that you messed up. You can do that by making a very public announcement saying: [...example given]" – Ryan Lundy Sep 27 '11 at 19:55
  • BTW, I just discovered that "git fetch --all -t" and "git fetch --all && git fetch -t" does not return the same result. The first one does not get all tags, while the second one does. – Sergi Oct 26 '11 at 15:55
24

I'm not a git guru, but I had used something like this before and it seemed to have worked fine:

git pull (or fetch, just need to make sure you are updated)
git checkout -b test remotes/origin/deploy
Skäggiga Mannen
  • 984
  • 7
  • 16
  • this is not working for me (MacOs X, remote is hosted at github) I get: fatal: Cannot update paths and switch to branch '6.2.3-ga4' at the same time. – igor.beslic Oct 01 '15 at 10:16
  • 2
    Make sure the branch you are trying to track exists. i.e. first do a git fetch, or git pull, or git remote update, etc.. – Skäggiga Mannen Oct 01 '15 at 13:23
5

You need to run

git pull
git checkout -b <new-branch-name> remotes/origin/<source-branch-name>
Community
  • 1
  • 1
0

to list all the tags

git fetch
git tags -l 

to create a local branch that points to the tag

git checkout tags/<tag_name> -b <branch_name>
git checkout -b <branch_name> tags/<tag_name>
ovi
  • 460
  • 4
  • 18