threads / discuss / 25899

Proper way to checkout a tag?

Subject: Proper way to checkout a tag?

## tl;dr

4 messages between Dec 1, 2010 and Dec 2, 2010.

replies: 3people: 3as markdown or json

Dun Peal· Dec 1, 2010, 19:38 UTC · lore

Hi. We tag our releases with names like "release-0", "release-1" and so forth. Tags (lightweight ones) seem like the perfect markers for these releases since we want a reference that can't ever change (as opposed to a head).

The problem is that since there's no head pointing to each release, if our users just:

$ git checkout release-1

When they want to build that release, they get into a detached HEAD state. I've always construed detached HEAD as a non-standard state, and it doesn't seem proper to reach it regularly in the course of a standard operation like checking out past releases.

It's particularly confusing for our users who are new to Git.
Any thoughts, or alternative methods to do this?
Thanks, D
Jonathan Nieder· Dec 1, 2010, 19:51 UTC · re: Dun Peal · lore

Re: Proper way to checkout a tag?

Hi,
Dun Peal wrote:
Show 11 quoted lines
> The problem is that since there's no head pointing to each release, if
> our users just:
> 
> $ git checkout release-1
> 
> When they want to build that release, they get into a detached HEAD
> state. I've always construed detached HEAD as a non-standard state,
> and it doesn't seem proper to reach it regularly in the course of a
> standard operation like checking out past releases.
> 
> It's particularly confusing for our users who are new to Git.
Any ideas for making the detached state more friendly?

The idea is this: when you check out a tag or a remote-tracking branch, it is not to make changes to it. Tags are unchanging, remote-tracking branches track remote state that the user does not directly control. So what does it mean to check out such a thing?

There are two possibilities:

If you are checking out that commit to examine it, test it, maybe write a few one-off patches, then any work built from there does not need to be remembered (except in the HEAD reflog, to avoid expensive accidents). You can make an experiment permanent with

	git checkout -b newbranch

If you are starting a new line of development, it needs to get a name. Long ago this was the only use case supported. Even today it is often a good way to go:

	git checkout -b newbranch release-1

Hope that helps. Jonathan

Dun Peal· Dec 1, 2010, 20:16 UTC · re: Jonathan Nieder · lore

Re: Proper way to checkout a tag?

On Wed, Dec 1, 2010 at 1:51 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> The idea is this: when you check out a tag or a remote-tracking
> branch, it is not to make changes to it.  Tags are unchanging,
> remote-tracking branches track remote state that the user does not
> directly control.

Yes, users checkout a release tag just so they can build parts of it. There's definitely no intention of creating new changes on top of them, and if there is then it should properly be a head (branch).

I guess that's exactly the use-case for detached HEAD, so I guess the answer is that we should all stop being afraid of that superficially scary term.

Thanks, D
Michael J Gruber· Dec 2, 2010, 08:33 UTC · re: Dun Peal · lore

Re: Proper way to checkout a tag?

Dun Peal venit, vidit, dixit 01.12.2010 21:16:
Show 13 quoted lines
> On Wed, Dec 1, 2010 at 1:51 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:
>> The idea is this: when you check out a tag or a remote-tracking
>> branch, it is not to make changes to it.  Tags are unchanging,
>> remote-tracking branches track remote state that the user does not
>> directly control.
> 
> Yes, users checkout a release tag just so they can build parts of it.
> There's definitely no intention of creating new changes on top of
> them, and if there is then it should properly be a head (branch).
> 
> I guess that's exactly the use-case for detached HEAD, so I guess the
> answer is that we should all stop being afraid of that superficially
> scary term.

It's really one of the most useful features of git (as Jonathan explained). There are two things which make it scary:

- The name. We could call it differently (free head, unbound head,
branchless head, west coast head...).
- The garbage collection. It's easy to commit on top of a detached head
by mistake, and once you switch away from that, it's difficult to find
it again (reflog) and easy to lose (gc/prune).

Though the "throw-away" nature of detached heads is a useful feature, we could possibly help users who commit on top of them better.

Michael

← back to recent threads