2021年1月23日 星期六

[Git 文章收集] How To Checkout Git Tags

 Source From Here

Preface
When working with Git, it is quite common for developers to create tags in order to have reference points in your development.

Tags are created in order to have references to release versions for example. Furthermore, tags are Git objects meaning that they can be checked out like you would check out a branch or a commit for example.

In this tutorial, we are going to see how you can checkout Git tags easily.

Checkout Git Tag
In order to checkout a Git tag, use the “git checkout” command and specify the tagname as well as the branch to be checked out:
  1. git checkout tags/<tag> -b <branch>  
Example:
# git log --oneline
c94e757 (HEAD -> master) add date into test.txt
bb134c5 (tag: v0.0.1, origin/master, origin/HEAD) Update 105M.img
faa8827 Upload large file >50M
66f0f1a Another commmit for merge testing
...


# git checkout tags/v0.0.1 -b my_branch
Switched to a new branch 'my_branch'

# git log --oneline | head -n 1 // Now we are at commit bb134c5 which has tag v0.0.1
bb134c5 Update 105M.img

Note that you will have to make sure that you have the latest tag list from your remote repository. To fetch tags from your remote repository, use “git fetch” with the “–all” and the “–tags” options:
# git pull -all -tags
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 1 (delta 0), pack-reused 0
Unpacking objects: 100% (1/1), 142 bytes | 142.00 KiB/s, done.
From https://github.com/johnklee/GitPro
* [new tag] v0.0.1 -> v0.0.1
Already up to date.

You can inspect the state of your branch by using the “git log” command. Make sure that the HEAD pointer (the latest commit) is pointing to your annotated tag.


Checkout latest Git tag
In some cases, you may be interested in checking out the latest Git tag of your repository. In order to checkout the latest Git tag, first update your repository by fetching the remote tags available.
# git fetch --tags
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 8 (delta 2), reused 8 (delta 2), pack-reused 0
Unpacking objects: 100% (8/8), 762 bytes | 127.00 KiB/s, done.
From https://github.com/johnklee/GitPro
bb134c5..783a307 master -> origin/master
* [new tag] v0.0.2 -> v0.0.2

As you can see, you retrieve the tag from your remote repository. Then, retrieve the latest tag available by using the “git describe” command:
# git log --oneline
783a307 (HEAD -> master, origin/master, origin/HEAD) commit after v0.0.2
ad16543 (tag: v0.0.2) commit for v0.0.2
bb134c5 (tag: v0.0.1, my_branch) Update 105M.img
faa8827 Upload large file >50M
...


# git rev-list --tags --max-count=1
ad16543cd4d8252ec2faf3b743fea1cc94550963

# git describe --tags `git rev-list --tags --max-count=1`
v0.0.2

# tag=$(git describe --tags `git rev-list --tags --max-count=1`)
# echo $tag
v0.0.2

# git checkout $tag -b latest
Switched to a new branch 'latest'

# git log --oneline --graph // Confirm the result
...

That’s it! You have successfully checkout the latest Git tag available in a new branch.

Supplement
2.6 Git 基礎 - 標籤

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...