Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is git tag, How to create tags & How to checkout git remote tag(s)

when I checkout remote git tag use command like this:

git checkout -b local_branch_name origin/remote_tag_name 

I got error like this:

error: pathspec origin/remote_tag_name did not match any file(s) known to git.

I can find remote_tag_name when I use git tag command.

like image 316
Ryanqy Avatar asked Mar 14 '16 04:03

Ryanqy


People also ask

What's a tag in git?

Tags are ref's that point to specific points in Git history. Tagging is generally used to capture a point in history that is used for a marked version release (i.e. v1. 0.1). A tag is like a branch that doesn't change. Unlike branches, tags, after being created, have no further history of commits.

How do I get git tags?

Find Latest Git Tag Available In order to find the latest Git tag available on your repository, you have to use the “git describe” command with the “–tags” option. This way, you will be presented with the tag that is associated with the latest commit of your current checked out branch.


1 Answers

Let's start by explaining what a tag in git is

enter image description here

A tag is used to label and mark a specific commit in the history.
It is usually used to mark release points (eg. v1.0, etc.).

Although a tag may appear similar to a branch, a tag, however, does not change. It points directly to a specific commit in the history and will not change unless explicitly updated.

enter image description here


You will not be able to checkout the tags if it's not locally in your repository so first, you have to fetch the tags to your local repository.

First, make sure that the tag exists locally by doing

# --all will fetch all the remotes. # --tags will fetch all tags as well $ git fetch --all --tags --prune 

Then check out the tag by running

$ git checkout tags/<tag_name> -b <branch_name> 

Instead of origin use the tags/ prefix.


In this sample you have 2 tags version 1.0 & version 1.1 you can check them out with any of the following:

$ git checkout A  ... $ git checkout version 1.0  ... $ git checkout tags/version 1.0  ... 

All of the above will do the same since the tag is only a pointer to a given commit.

enter image description here
origin: https://backlog.com/git-tutorial/img/post/stepup/capture_stepup4_1_1.png


How to see the list of all tags?

# list all tags $ git tag  # list all tags with given pattern ex: v- $ git tag --list 'v-*' 

How to create tags?

There are 2 ways to create a tag:

# lightweight tag  $ git tag   # annotated tag $ git tag -a 

The difference between the 2 is that when creating an annotated tag you can add metadata as you have in a git commit:
name, e-mail, date, comment & signature

enter image description here

How to delete tags?

Delete a local tag

$ git tag -d <tag_name> Deleted tag <tag_name> (was 000000) 

Note: If you try to delete a non existig Git tag, there will be see the following error:

$ git tag -d <tag_name> error: tag '<tag_name>' not found. 

Delete remote tags

# Delete a tag from the server with push tags $ git push --delete origin <tag name> 

How to clone a specific tag?

In order to grab the content of a given tag, you can use the checkout command. As explained above tags are like any other commits so we can use checkout and instead of using the SHA-1 simply replacing it with the tag_name

Option 1:

# Update the local git repo with the latest tags from all remotes $ git fetch --all  # checkout the specific tag $ git checkout tags/<tag> -b <branch> 

Option 2:

Using the clone command

Since git supports shallow clone by adding the --branch to the clone command we can use the tag name instead of the branch name. Git knows how to "translate" the given SHA-1 to the relevant commit

# Clone a specific tag name using git clone  $ git clone <url> --branch=<tag_name> 

git clone --branch=

--branch can also take tags and detaches the HEAD at that commit in the resulting repository.


How to push tags?

git push --tags

To push all tags:

# Push all tags $ git push --tags  

Using the refs/tags instead of just specifying the <tagname>.

Why?

  • It's recommended to use refs/tags since sometimes tags can have the same name as your branches and a simple git push will push the branch instead of the tag

To push annotated tags and current history chain tags use:

git push --follow-tags

This flag --follow-tags pushes both commits and only tags that are both:

  • Annotated tags (so you can skip local/temp build tags)
  • Reachable tags (an ancestor) from the current branch (located on the history)

enter image description here

From Git 2.4 you can set it using configuration

$ git config --global push.followTags true 

Cheatsheet: enter image description here


like image 137
CodeWizard Avatar answered Sep 21 '22 12:09

CodeWizard