本文以 Github 上的 logstash-filter-geoip 项目为例,演示如何将代码切换到指定的 Tag 上。


使用 git clone 从 Github 上将 logstash-filter-geoip 代码克隆至本地:

1
git clone https://github.com/logstash-plugins/logstash-filter-geoip.git

进入代码目录:

1
cd logstash-filter-geoip

使用 git tag 查看所有的 Tag:

1
git tag

结果如下(注意,显示的标签列表不是按标签创建时间顺序来排序的,而是按字母排序的):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
v0.1.0
v0.1.1
v0.1.10
v0.1.2
......
v7.2.1
v7.2.10
v7.2.11
v7.2.12
v7.2.13
v7.2.2
v7.2.3
v7.2.4
v7.2.5
v7.2.6
v7.2.7
v7.2.8
v7.2.9

使用 git checkout 切换至标签 v7.2.13:

1
git checkout v7.2.13

结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
➜  logstash-filter-geoip git:(main) git checkout v7.2.13
Note: switching to 'v7.2.13'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

git switch -c <new-branch-name>

Or undo this operation with:

git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at fc2df01 [DOC] Add documentation for database downloader setting (#210)
➜ logstash-filter-geoip git:(fc2df01)

从提示中可以看出当前处于一个“detached HEAD”状态,不允许修改代码,因为标签也是版本库的一个快照。如果想要在某个 Tag 代码的基础上做修改,可以在这个 Tag 代码基础上创建一个分支:

1
git checkout -b branch_name tag_name

比如在 v7.2.13 在基础上创建分支 hotfix:

1
git checkout -b hotfix v7.2.13

后续在这个分支上的操作,就和普通的分支 git 操作一样了。

(END)