Git 的修订版号相当于什么?
我们在工作中使用 SVN,但对于我的个人项目,我决定使用 Git。所以我昨天安装了 Git,我想知道 Git 中的修订号相当于什么。
假设我们使用的是 3.0.8 版本,每个错误修复都有自己的修订号,当我们谈论此错误修复时可以使用。那么,如果我将 Git 中的代码标记为 3.0.8,那么我可以使用什么作为修订号或其他更详细的标识?我发现哈希值对人类来说不太友好。
We use SVN at work, but for my personal projects I decided to use Git. So I installed Git yesterday, and I wonder what is the revision number equivalent in Git.
Let's say we work on version 3.0.8 and every bug fix has its own revision number we can use when we talk about this bug fix. So if I tag the code in Git to 3.0.8 what then I can use as a revision number or some other more detailed kind of identification? I find the hash not so user friendly for humans.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
使用现代 Git(我的例子是 1.8.3.4)并且不使用分支,您可以这样做:
但这有各种各样的问题,并且可能不容易重现或容易返回到提交哈希(如果您需要)。因此,请尽量避免使用它或仅将其用作提示。
With modern Git (1.8.3.4 in my case) and not using branches you can do:
But this has all kinds of issues and might not be easy to reproduce or easy to get back to the commit hash in case you need to. So try to avoid it or use it only as a hint.
对您来说是好消息还是坏消息,该哈希值就是修订号。当我从 SVN 切换到 git 时,我也遇到了这个问题。
您可以使用 git 中的“标记”将某个修订版标记为特定版本的“发布版”,以便轻松引用该修订版。查看这篇博客文章。
要理解的关键是 git 不能有修订号 - 考虑分散的性质。如果用户 A 和 B 都提交到他们的本地存储库,那么 git 如何合理地分配连续的修订号? A 在推送/拉取对方的更改之前并不了解 B。
另一件需要注意的事情是错误修复分支的简化分支:
从版本开始:3.0.8。然后,在该版本发布后,执行以下操作:
这将为错误修复创建一个分支。检查分支:
现在进行您想要的任何错误修复更改。
提交它们,然后切换回主分支:
然后从另一个分支中提取这些更改:
这样,您就有了一个单独的特定于版本的错误修复分支,但您仍然将错误修复更改拉入主开发主干。
Good or bad news for you, that hash IS the revision number. I also had trouble with this when I made the switch from SVN to git.
You can use "tagging" in git to tag a certain revision as the "release" for a specific version, making it easy to refer to that revision. Check out this blog post.
The key thing to understand is that git cannot have revision numbers - think about the decentralized nature. If users A and B are both committing to their local repositories, how can git reasonably assign a sequential revision number? A has no knowledge of B before they push/pull each other's changes.
Another thing to look at is simplified branching for bugfix branches:
Start with a release: 3.0.8. Then, after that release, do this:
This will create a branch for bugfixes. Checkout the branch:
Now make any bugfix changes you want.
Commit them, and switch back to the master branch:
Then pull in those changes from the other branch:
That way, you have a separate release-specific bugfix branch, but you're still pulling the bugfix changes into your main dev trunk.
git describe
命令创建一个稍微更容易理解的名称指的是特定的提交。例如,从文档中:只要您使用合理命名的标签来标记特定版本,就可以认为这大致相当于 SVN“修订号”。
The
git describe
command creates a slightly more human readable name that refers to a specific commit. For example, from the documentation:As long as you use sensibly named tags to tag particular releases, this can be considered to be roughly equivalent to a SVN "revision number".
其他海报是对的,没有“修订号”。
我认为最好的方法是使用标签来“发布”!
但我使用以下内容来伪造修订号(只是为了让客户看到修订和进度,因为他们希望从 git 获得与 subversion 相同的不断增加的修订)。
显示“HEAD”的“当前修订版”是通过使用以下
命令模拟的: git rev-list HEAD | wc -l
但是如果客户告诉我“revision”1302 有错误怎么办?
为此,我将以下内容添加到 ~/.gitconfig 的 [alias] 部分:
show-rev-number = !sh -c 'git rev-list --reverse HEAD |荷兰 | awk \"{ if(\\$1 == "$0") { print \\$2 }}\"'
使用
git show-rev-number 1302
将打印 哈希“修订”:)我做了一个博客文章(德语)关于不久前的“技术”。
The other posters are right, there is no "revision-number".
I think the best way is to use Tags for "releases"!
But I made use of the following to fake revision numbers (just for clients to see revisions and the progress, as they wanted to have the same increasing revisions from git as they where use to by subversion).
Show the "current revision" of "HEAD" is simulated by using this:
git rev-list HEAD | wc -l
But what if the client tells me that there is a bug in "revision" 1302 ?
For this I added the following to the [alias] section of my ~/.gitconfig:
show-rev-number = !sh -c 'git rev-list --reverse HEAD | nl | awk \"{ if(\\$1 == "$0") { print \\$2 }}\"'
using
git show-rev-number 1302
will then print the hash for the "revision" :)I made a Blog Post (in german) about that "technique" some time ago.
Git 没有与 subversion 相同的修订号概念。相反,通过提交创建的每个给定快照都由 SHA1 校验和标记。为什么?在分布式版本控制系统中运行 revno 存在几个问题:
首先,由于开发根本不是线性的,因此数字的附加问题很难以满足程序员需求的方式解决。当数字的行为不符合您的预期时,尝试通过添加数字来解决此问题可能很快就会出现问题。
其次,修订号可能在不同的机器上生成。这使得数字同步变得更加困难 - 特别是因为连接是单向的;您甚至可能无法访问拥有该存储库的所有计算机。
第三,在 git 中,在某种程度上是由现已不复存在的 OpenCM 系统开创的,提交的身份(提交是什么)相当于其名称(SHA id)。这个命名=身份的概念非常强烈。当您手头有提交名称时,它还会以不可伪造的方式标识该提交。这反过来又可以让您使用 git fsck 命令检查所有提交回到第一个初始提交是否损坏。
现在,由于我们有修订版的 DAG(有向无环图)并且它们构成了当前树,因此我们需要一些工具来解决您的问题:我们如何区分不同的版本。首先,如果给定的前缀(例如 1516bd)唯一标识您的提交,您可以省略部分哈希值。但这也是相当做作的。相反,技巧是使用标签和/或分支。标签或分支类似于您附加到给定提交 SHA1-id 的“黄色便笺”。本质上,标签是不移动的,而当新的提交提交到分支的 HEAD 时,分支就会移动。有多种方法可以引用标签或分支周围的提交,请参阅 git-rev-parse 的手册页。
通常,如果您需要处理特定的代码片段,那么该代码片段正在进行更改,因此应该是一个具有特定主题名称的分支。创建大量分支(每个程序员 20-30 个分支并非闻所未闻,其中发布了 4-5 个分支供其他人使用)是有效 git 的秘诀。每一项工作都应该作为自己的分支开始,然后在测试时合并。未发布的分支可以完全重写,这部分破坏历史就是 git 的力量。
当改变被主人接受时,它就会冻结并成为考古学。此时,您可以对其进行标记,但更常见的是通过 sha1 和在错误跟踪器或问题跟踪器中对特定提交进行引用。标签往往保留用于版本升级和维护分支(对于旧版本)的分支点。
Git does not have the same concept of revision numbers as subversion. Instead each given snapshot made with a commit is tagged by a SHA1 checksum. Why? There are several problems with a running revno in a distributed version control system:
First, since development is not linear at all, the attachment of a number is rather hard as a problem to solve in a way which will satisfy your need as a programmer. Trying to fix this by adding a number might quickly become problematic when the number does not behave as you expect.
Second, revision numbers may be generated on different machines. This makes synchronization of numbers much harder - especially since connectivity is one-way; you may not even have access to all machines that has the repository.
Third, in git, somewhat pioneered by the now defunct OpenCM system, the identity of a commit (what the commit is) is equivalent to its name (the SHA id). This naming = identity concept is very strong. When you sit with a commit name in hand it also identifies the commit in an unforgeable way. This in turn lets you check all of your commits back to the first initial one for corruption with the
git fsck
command.Now, since we have a DAG (Directed Acyclic Graph) of revisions and these constitute the current tree, we need some tools to solve your problem: How do we discriminate different versions. First, you can omit part of the hash if a given prefix, 1516bd say, uniquely identifies your commit. But this is also rather contrived. Instead, the trick is to use tags and or branches. A tag or branch is akin to a "yellow stick it note" you attach to a given commit SHA1-id. Tags are, in essence, meant to be non-moving whereas a branch will move when new commits are made to its HEAD. There are ways to refer to a commit around a tag or branch, see the man page of git-rev-parse.
Usually, if you need to work on a specific piece of code, that piece is undergoing changes and should as such be a branch with a saying topic name. Creating lots of branches (20-30 per programmer is not unheard of, with some 4-5 published for others to work on) is the trick for effective git. Every piece of work should start as its own branch and then be merged in when it is tested. Unpublished branches can be rewritten entirely and this part of destroying history is a force of git.
When the change is accepted into master it somewhat freezes and becomes archeology. At that point, you can tag it, but more often a reference to the particular commit is made in a bug tracker or issue tracker via the sha1 sum. Tags tend to be reserved for version bumps and branch points for maintenance branches (for old versions).
如果您有兴趣,我会从 git infos 此处 自动管理版本号,格式为
构建总数提交次数。您将在
Makefile
。以下是访问版本号不同部分的相关部分:If you're interested, I managed version numbers automatically from git infos here under the format
where build is the total number of commits. You'll see the interesting code in the
Makefile
. Here is the relevant part to access the different part of the version number:Bash 函数:
输出类似的
内容
A Bash function:
outputs something like
That is
提交的 SHA1 哈希 相当于 Subversion 修订版号。
The SHA1 hash of the commit is the equivalent to a Subversion revision number.
这是我根据其他解决方案在 makefile 中所做的。请注意,这不仅会为您的代码提供修订号,还会附加允许您重新创建版本的哈希值。
This is what I did in my makefile based on others solutions. Note not only does this give your code a revision number, it also appends the hash which allows you to recreate the release.
使用 git hash 作为构建号的问题是它不是单调递增的。 OSGi 建议对内部版本号使用时间戳。看起来分支的提交数量可以用来代替颠覆或强制更改数量。
The problem with using the git hash as the build number is that it's not monotonically increasing. OSGi suggests using a time-stamp for the build number. It looks like the number of commits to the branch could be used in place of the subversion or perforce change number.
我编写了一些 PowerShell 实用程序,用于从 Git 检索版本信息并简化标记
功能:Get-LastVersion、Get-Revision、Get-NextMajorVersion、Get-NextMinorVersion、TagNextMajorVersion、TagNextMinorVersion:
I wrote some PowerShell utilities for retrieving version information from Git and simplifying tagging
functions: Get-LastVersion, Get-Revision, Get-NextMajorVersion, Get-NextMinorVersion, TagNextMajorVersion, TagNextMinorVersion:
每个提交都有一个唯一的哈希值。除此之外,git 中没有修订号。如果您想要更多的用户友好性,您必须自己标记提交。
Each commit has a unique hash. Other than that there are no revision numbers in git. You'll have to tag commits yourself if you want more user-friendliness.
我只想指出另一种可能的方法 - 那就是使用 git git-notes(1),自 v 1.6.6 起存在(自我注释 - Git)(我使用的是
git
版本 1.7.9.5)。基本上,我使用 git svn 克隆具有线性历史记录的 SVN 存储库(没有标准布局,没有分支,没有标签),并且我想比较克隆的 git 中的修订号> 存储库。这个 git 克隆默认情况下没有标签,所以我无法使用
gitdescribe
。这里的策略可能只适用于线性历史 - 不确定合并等会如何结果;但这是基本策略:rev-list
默认情况下按“逆时间顺序”排列,因此我们将使用其--reverse
开关来获取按最旧的顺序排序的提交列表< /里>git log
和-- 来浏览日志Notes
,这也会转储提交的注释,在这种情况下将是“修订号”git status
中)首先,让我们注意
git
有一个默认的注释位置 - 但您也可以指定一个>ref
(erence) 用于注释 - 它将它们存储在.git
下的不同目录中;例如,在git
repo 文件夹中,您可以调用git Notes get-ref
来查看该目录:需要注意的是,如果您 < code>notes add 带有
--ref
,之后您还必须再次使用该引用 - 否则您可能会收到类似“找不到对象 XXX 的注释...< /em>”。对于此示例,我选择将注释的
ref
称为“linrev”(用于线性修订) - 这也意味着该过程不太可能干扰已有的注释。我还使用--git-dir
开关,因为作为git
新手,我在理解它时遇到了一些问题 - 所以我想“稍后记住” <代码>:);我还使用--no-pager
在使用git log
时抑制less
的产生。因此,假设您位于一个目录中,其子文件夹
myrepo_git
是一个git
存储库;可以这样做:因此,至少在我没有分支的完全线性历史的特定情况下,修订号似乎与这种方法相匹配 - 另外,这种方法似乎允许使用 git log 。具有修订范围,同时仍然获得正确的修订号 - YMMV 具有不同的上下文,但是......
希望这对某人有帮助,
干杯!
编辑:好的,这里更容易一些,使用上述循环的 git 别名,称为 setlinrev 和 unsetlinrev ;在 git 存储库文件夹中时,执行 (注意令人讨厌的
bash
转义,另请参阅 #16136745 - 添加包含分号的 Git 别名):...这样你就可以简单地调用
git setlinrev
在尝试做涉及线性修订笔记的日志之前;完成后使用 git unsetlinrev 删除这些注释; git repo 目录内部的一个示例:shell 完成这些别名所需的时间取决于存储库历史记录的大小。
I'd just like to note another possible approach - and that is by using
git
git-notes(1), in existence since v 1.6.6 (Note to Self - Git) (I'm usinggit
version 1.7.9.5).Basically, I used
git svn
to clone an SVN repository with linear history (no standard layout, no branches, no tags), and I wanted to compare revision numbers in the clonedgit
repository. This git clone doesn't have tags by default, so I cannot usegit describe
. The strategy here likely would work only for linear history - not sure how it would turn out with merges etc.; but here is the basic strategy:git rev-list
for list of all commit historyrev-list
is by default in "reverse chronological order", we'd use its--reverse
switch to get list of commits sorted by oldest firstbash
shell togit log
with--notes
, which will also dump a commit's note, which in this case would be the "revision number"git status
)First, let's note that
git
has a default location of notes - but you can also specify aref
(erence) for notes - which would store them in a different directory under.git
; for instance, while in agit
repo folder, you can callgit notes get-ref
to see what directory that will be:The thing to be noted is that if you
notes add
with a--ref
, you must also afterwards use that reference again - otherwise you may get errors like "No note found for object XXX...".For this example, I have chosen to call the
ref
of the notes "linrev" (for linear revision) - this also means it is not likely the procedure will interfere with already existing notes. I am also using the--git-dir
switch, since being agit
newbie, I had some problems understanding it - so I'd like to "remember for later":)
; and I also use--no-pager
to suppress spawning ofless
when usinggit log
.So, assuming you're in a directory, with a subfolder
myrepo_git
which is agit
repository; one could do:So, at least in my specific case of fully linear history with no branches, the revision numbers seem to match with this approach - and additionally, it seems that this approach will allow using
git log
with revision ranges, while still getting the right revision numbers - YMMV with a different context, though...Hope this helps someone,
Cheers!
EDIT: Ok, here it is a bit easier, with
git
aliases for the above loops, calledsetlinrev
andunsetlinrev
; when in your git repository folder, do (Note the nastybash
escaping, see also #16136745 - Add a Git alias containing a semicolon):... so you can simply invoke
git setlinrev
before trying to do log involving linear revision notes; andgit unsetlinrev
to delete those notes when you're done; an example from inside the git repo directory:The time it would take the shell to complete these aliases, would depend on the size of the repository history.
对于拥有 Ant 构建过程的人,您可以在 git 上为项目生成版本号使用此目标:
结果如下所示:
当您在生成版本号时有未提交的文件时,脏标志就会出现。因为通常,当您构建/打包应用程序时,每个代码修改都必须位于存储库中。
For people who have an Ant build process, you can generate a version number for a project on git with this target:
The result looks like this:
The dirty flag is here when you have file(s) not committed when you generate the version number. Because usually, when you build/package your application, every code modification has to be in the repository.
我们正在使用此命令从 git 获取版本和修订版:
它返回
gcc7b71f
),v2.1.0
code>,用于发布)v5.3.0-88-gcc7b71f
)之后的提交哈希值,v5.3.0-88-gcc7b71f-dirty
)另请参阅:https://www.git-scm.com/docs/git-describe#Documentation/git-describe.txt
We're using this command to get version and revision from git:
It returns
gcc7b71f
)v2.1.0
, used for releases)v5.3.0-88-gcc7b71f
)v5.3.0-88-gcc7b71f-dirty
)See also: https://www.git-scm.com/docs/git-describe#Documentation/git-describe.txt
连同提交的 SHA-1 id、服务器时间的日期和时间会有帮助吗?
像这样的东西:
Along with the SHA-1 id of the commit, date and time of the server time would have helped?
Something like this:
从 Git 手册来看,标签是这个问题的一个很好的答案:
查看 2.6 Git 基础知识 - 标记
From the Git manual, tags are a brilliant answer to this issue:
Check out 2.6 Git Basics - Tagging
Visual Studio 的构建后事件
Post build event for Visual Studio
考虑使用
git-rev-label
提供信息关于 Git 存储库修订版,格式如
master-c73-gabc6bec
。可以使用来自 Git 的环境变量和信息填充模板字符串或文件。
有助于提供有关程序版本的信息:分支、标签、提交哈希、
提交计数、脏状态、日期和时间。最有用的事情之一是计数
提交,不考虑合并分支 - 仅第一个父级。
Consider to use
git-rev-label
Gives information about Git repository revision in format like
master-c73-gabc6bec
.Can fill template string or file with environment variables and information from Git.
Useful to provide information about version of the program: branch, tag, commit hash,
commits count, dirty status, date and time. One of the most useful things is count of
commits, not taking into account merged branches - only first parent.