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

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


当前回答

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

其他回答

树枝是木头做的,从树干上长出来。标签由纸(木材的衍生品)制成,像圣诞装饰品一样挂在树上的各个地方。

您的项目是树,您将添加到项目中的特性将在分支上生长。答案是分支。

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

分支机构

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

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

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

Tags

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

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

简单:

标签总是指向项目的同一个版本,而头部则随着开发的进展而变化。

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".

标签可以是有符号的,也可以是无符号的;分支从不签名。

有签名的标记永远不能移动,因为它们以加密方式(使用签名)绑定到特定的提交。Unsigned标签是不绑定的,可以移动它们(但是移动标签不是一个正常的用例)。

分支不仅可以移动到不同的提交,而且还可以这样做。您应该为本地开发项目使用分支。“在标记上”将工作提交到Git存储库是没有意义的。