如何在Git中管理版本号? [英] How to manage the version number in Git?

查看:101
本文介绍了如何在Git中管理版本号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们想象一下 blepp 命令行工具"class =" post-tag"title ="显示标记为'git'的问题"rel =" tag> git .此工具具有(隐藏的)--version选项,该选项返回其版本(假设)和另一个--commit,它返回从其构建的提交编号.

Let's imagine the blerp command line tool maintained on git. This tool has the (hidden) --version option which returns its version (let's say 0.1.2) and another --commit which returns the commit number from which it was built.

版本和提交号均在代码库中硬编码.

Both the version and the commit number are hard-coded on the code base.

现在我进行错误修复,然后提交并重建程序.我仍然会看到0.1.2,尽管此新版本与原始版本0.1.2不同.只有提交会告诉我它不是相同的0.1.2.该错误修正值得一个不同的版本号吗?

Now I make a bugfix then commit and rebuild my program. I will still see 0.1.2 although this new version differ from the original 0.1.2. Only the commit will tell me that it is not the same 0.1.2. Is that bugfix worth a different version number?

一种解决方案是,每次提交时,我都会增加硬编码的版本号(这意味着每次提交总是至少修改2个文件).这是一个绑定解决方案,当开发人员在不同的活动分支上工作时,它将不起作用.如果Bob在版本0.1.2中使用功能foo,而Alice在相同版本中使用功能bar.他们如何增加版本号?鲍勃可以使用奇数,爱丽丝可以使用偶数.如果Eve可以使用第三项功能怎么办?

One solution is that each time I make a commit, I increase the hard-coded version number (which implies to always modify a minimum of 2 files for each commit). This is a binding solution and it does not work when the developers are working on different active branches. If Bob works on feature foo from the version 0.1.2 and Alice works on feature bar from the same version. How do they increase their version number? Bob can use the odd and Alice the even. What if Eve works on a third feature?

另一种解决方案是使用Git标签自动生成版本号.脚本可以找到以v开头的最近标签,例如v0.1.2,并将标签名称用作版本号加上当前提交的前n位数字(v0.1.2 (build 4acd21)).如果工作目录是干净的,这将很好地工作.可以想象在内部版本号之前添加*来指示工作目录不干净.此解决方案的主要问题是,如果有人导出源,将无法构建 blerp .

Another solution can be to use the Git tags to automatically generate the version number. A script can find the closest tag starting with v such as v0.1.2 and use the tag name as the version number plus the first n digits of the current commit (v0.1.2 (build 4acd21)). This works well if the working directory is clean. One can imagine to add a * before the build number to indicate the working directory is not clean. The main problem with this solution is if somebody export the sources, it won't be able to build blerp.

有什么可能的替代方法可以解决此问题?

What possible alternative can solve this issue?

推荐答案

亚历山大·基瑟列夫(Alexey Kiselev)和达里奥(Dario)已经暗示要回答这个问题,但我将尝试对其进行详细解释.

Alexey Kiselev and Dario already hinted towards the answer, but I will try to explain it in detail.

有两种类型的版本控制方案:

There are two types of versioning schemes:

  1. 内部版本号:一天之内可以增加多次(例如版本控制号)
  2. 已发布的版本:更改频率较低(例如语义版本控制)

人们根据需要使用不同的方案,但是

People use different schemes as per their need, but semantic versioning is fairly widely used and authored by Tom Preston-Werner, co-founder of GitHub.

语义版本控制遵循X.Y.Z

或更易读的将是[major].[minor].[patch]-[build/beta/rc]

例如1.2.0-beta

major or X可以递增.

minor or Y会递增.

patch or Z会增加.

通过使用标签:

Git中的标签可用于添加版本号.

Tags in Git can be used to add a version number.

git tag -a "v1.5.0-beta" -m "version v1.5.0-beta"

将v1.5.0-beta的版本标签添加到您当前的Git存储库中.此后的每个新提交都将通过追加提交编号和提交哈希值来自动递增标记.可以使用git describe命令查看.

adds a version tag of v1.5.0-beta to your current Git repository. Every new commit after this will auto-increment tag by appending commit number and commit hash. This can be viewed using the git describe command.

v1.5.0-beta-1-g0c4f33f,其中-1-是提交编号,而0c4f33f是提交哈希的缩写. g前缀代表"git".

v1.5.0-beta-1-g0c4f33f here -1- is the commit number and 0c4f33f the abbreviation of commit's hash. The g prefix stands for "git".

可以使用以下方法查看完整的详细信息:

Complete details can be viewed using:

git show v1.5.0-beta

这篇关于如何在Git中管理版本号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆