我有一些困难理解如何使用标签与分支在git。

我只是将当前版本的代码从cvs移到git,现在我将针对特定的特性处理该代码的一个子集。其他一些开发人员也将致力于此,但并不是我们团队中的所有开发人员都会关心此功能。我应该创建一个分支还是一个标签?在什么情况下我应该使用其中一种而不是另一种?


当前回答

标记用于标记版本,更具体地说,它引用分支上的一个时间点。分支通常用于向项目中添加特性。

其他回答

从理论角度看:

标签是给定修订的符号名称。它们总是指向相同的对象(通常是:指向相同的修订);它们不会改变。 分支是开发线的象征性名称。在分支的顶部创建新的提交。分支指针自然地向前移动,指向越来越新的提交。


从技术角度看:

tags reside in refs/tags/ namespace, and can point to tag objects (annotated and optionally GPG signed tags) or directly to commit object (less used lightweight tag for local names), or in very rare cases even to tree object or blob object (e.g. GPG signature). branches reside in refs/heads/ namespace, and can point only to commit objects. The HEAD pointer must refer to a branch (symbolic reference) or directly to a commit (detached HEAD or unnamed branch). remote-tracking branches reside in refs/remotes/<remote>/ namespace, and follow ordinary branches in remote repository <remote>.


参见gitglossary manpage:

branch A "branch" is an active line of development. The most recent commit on a branch is referred to as the tip of that branch. The tip of the branch is referenced by a branch head, which moves forward as additional development is done on the branch. A single git repository can track an arbitrary number of branches, but your working tree is associated with just one of them (the "current" or "checked out" branch), and HEAD points to that branch. tag A ref pointing to a tag or commit object. In contrast to a head, a tag is not changed by a commit. Tags (not tag objects) are stored in $GIT_DIR/refs/tags/. [...]. A tag is most typically used to mark a particular point in the commit ancestry chain. tag object An object containing a ref pointing to another object, which can contain a message just like a commit object. It can also contain a (PGP) signature, in which case it is called a "signed tag object".

没有什么比喻是完美的,但是您可以将您的存储库想象成一本记录项目进展的书。

分支机构

你可以把分支想象成一个粘性书签:

全新的存储库只有其中一个(称为master main),它自动移动到您所编写的最新页面(考虑提交)。但是,你可以自由地创建和使用更多的书签,以便标记书中其他感兴趣的点,这样你就可以快速返回到它们。

此外,你总是可以将一个特定的书签移动到书的其他页面(例如,使用git-reset);兴趣点通常随时间变化。

Tags

你可以把标签看作章节标题。

它可能包含标题(考虑带注释的标签),也可能不包含。标签与分支相似但又不同,因为它标记了书中历史兴趣的点。为了保持它的历史特性,一旦你共享了一个标签(即把它推到一个共享的遥控器上),你就不应该把它移动到书中的其他地方。

标记用于标记版本,更具体地说,它引用分支上的一个时间点。分支通常用于向项目中添加特性。

Git寓言解释了典型的DVCS是如何创建的,以及它们的创造者为什么要这样做。另外,你可能想看看Git for Computer Scientist;它解释了Git中每种类型的对象的功能,包括分支和标记。

标记表示特定分支在某一时刻的版本。分支代表一个独立的开发线程,它可以与相同代码库上的其他开发工作同时运行。对一个分支的更改最终可能合并回另一个分支以统一它们。

Usually you'll tag a particular version so that you can recreate it, e.g., this is the version we shipped to XYZ Corp. A branch is more of a strategy to provide on-going updates on a particular version of the code while continuing to do development on it. You'll make a branch of the delivered version, continue development on the main line, but make bug fixes to the branch that represents the delivered version. Eventually, you'll merge these bug fixes back into the main line. Often you'll use both branching and tagging together. You'll have various tags that may apply both to the main line and its branches marking particular versions (those delivered to customers, for instance) along each branch that you may want to recreate -- for delivery, bug diagnosis, etc.

它实际上比这更复杂——或者像你想的那样复杂——但这些例子应该能让你了解其中的区别。