Git - how to checkout a tag

In version management, Tag is used to mark and save a configuration state for tracking and backing up the configuration.

When a version is released, a Tag is generally created. Then use this Tag to refer to this version.

In Git, if you know a Tag state, how do you switch the configuration to that state locally?

Use the checkmout command and add the tag name to switch to the tag, similar to checking out a branch or commit. for example:

$ git checkout tags/<tag> -b <branch>

tags is a prefix, you can also not add it. There is also the following -b <branch>, which can also be omitted. If not added, it is a 'detached HEAD' state.

Note that this tag is the tag of the remote warehouse, and the local must have the latest tag list, including the tag that needs to be checked out.

How to update local tag list?

$ git fetch --all --tags 

Fetching origin 

From git-repository 

98a14be..7a9ad7f master -> origin/master 

* [new tag] v1.0 -> v1.0

When fetching tags here, you can also not add --all.

For example, if you want to checkout a "v1.0" tag, and create a new branch named "release":

$ git checkout tags/v1.0 -b v1.0-branch 

Switched to a new branch 'v1.0-branch'

Then use the log command to check the status of the local workspace, whether the switch is successful:

$ git log --oneline --graph 

* 53a7dcf (HEAD -> v1.0-branch, tag: v1.0) Version 1.0 commit 

* 0a9e448 added files 

* bd6903f (release) first commit

How to check out a newest tag?

1. Update the local Tag

$ git fetch --tags

2. Use the git describe command to get the latest tag:

$ tag=$(git describe --tags `git rev-list --tags --max-count=1`) 

$ echo $tag 

v2.0

3. Use the git checkout command to switch to the new branch:

$ git checkout $tag -b latest 

Switched to a new branch 'latest'

Then use the git log command to check whether the operation was successful.

reference:

1,How To Checkout Git Tags – devconnected

Guess you like

Origin blog.csdn.net/guoqx/article/details/130326591