Tagging versions

Goals

  • Learn how to tag commits with names for future reference

Let’s call the current version of the hello program version 1 (v1).

Tagging version 1

Execute:

  1. git tag v1

Now you can refer to the current version of the program as v1.

Tagging Previous Versions

Let’s tag the version immediately prior to the current version v1-beta. First we need to checkout the previous version. Rather than look up the hash, we will use the ^ notation to indicate “the parent of v1”.

If the v1^ notation gives you any trouble, you can also try v1~1, which will reference the same version. This notation means “the first ancestor of v1”.

Execute:

  1. git checkout v1^
  2. cat hello.rb

Output:

  1. $ git checkout v1^
  2. Note: checking out 'v1^'.
  3. You are in 'detached HEAD' state. You can look around, make experimental
  4. changes and commit them, and you can discard any commits you make in this
  5. state without impacting any branches by performing another checkout.
  6. If you want to create a new branch to retain commits you create, you may
  7. do so (now or later) by using -b with the checkout command again. Example:
  8. git checkout -b <new-branch-name>
  9. HEAD is now at c8b3af1 Added a default value
  10. $ cat hello.rb
  11. name = ARGV.first || "World"
  12. puts "Hello, #{name}!"

See, this is the version with the default value before we added the comment. Let’s make this v1-beta.

Execute:

  1. git tag v1-beta

Checking Out by Tag Name

Now try going back and forth between the two tagged versions.

Execute:

  1. git checkout v1
  2. git checkout v1-beta

Output:

  1. $ git checkout v1
  2. Previous HEAD position was c8b3af1 Added a default value
  3. HEAD is now at 4254c94 Added a comment
  4. $ git checkout v1-beta
  5. Previous HEAD position was 4254c94 Added a comment
  6. HEAD is now at c8b3af1 Added a default value

Viewing Tags using the tag command

You can see what tags are available using the git tag command.

Execute:

  1. git tag

Output:

  1. $ git tag
  2. v1
  3. v1-beta

Viewing Tags in the Logs

You can also check for tags in the log.

Execute:

  1. git hist master --all

Output:

  1. $ git hist master --all
  2. * 4254c94 2020-06-20 | Added a comment (tag: v1, master) [Jim Weirich]
  3. * c8b3af1 2020-06-20 | Added a default value (HEAD, tag: v1-beta) [Jim Weirich]
  4. * 30c2cd4 2020-06-20 | Using ARGV [Jim Weirich]
  5. * 4445720 2020-06-20 | First Commit [Jim Weirich]

You can see both tags (v1 and v1-beta) listed in the log output, along with the branch name (master). Also HEAD shows you the currently checked out commit (which is v1-beta at the moment).