git 如何使用某些提交创建标签并将其推送到源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25755061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create tag with certain commits and push it to origin?
提问by aznmunkey
Say the current log in my gerrit looks like the following:
假设我的 gerrit 中的当前日志如下所示:
- commit10 (master)
- commit9
- commit8
- commit7
- commit6 v1.72.0
- commit5
- commit4 v1.71.0
- commit3
- commit2
- commit1
- commit10(主)
- 提交9
- 提交8
- 提交7
- commit6 v1.72.0
- 提交5
- commit4 v1.71.0
- 提交3
- 提交2
- 提交1
My goal is to create a new tag (v1.73.0) that should contain commit8 and commit9 and push it to origin. I was told to create a new local branch based on the latest stable tag and cherry-pick the necessary commits and tag it up. However, I am having some problem pushing the tag up to master.
我的目标是创建一个应包含 commit8 和 commit9 的新标签 (v1.73.0) 并将其推送到原点。我被告知要基于最新的稳定标签创建一个新的本地分支,并挑选必要的提交并对其进行标记。但是,我在将标签推送到 master 时遇到了一些问题。
Here's what I've done:
这是我所做的:
- create local branch based on the latest tag: git checkout -b branchforv1.73.0 v1.72.0
- cherry-pick commit8 and commit9
- create new tag: git tag v1.73.0
- 根据最新标签创建本地分支: git checkout -b branchforv1.73.0 v1.72.0
- 挑选 commit8 和 commit9
- 创建新标签:git tag v1.73.0
...so now, how do I push v1.73.0 to master?
...那么现在,我如何将 v1.73.0 推送到 master?
Result:
结果:
- commit10 (master)
- commit7
- commit9 v1.73.0
- commit8
- commit6 v1.72.0
- commit5
- commit4 v1.71.0
- commit3
- commit2
- commit1
- commit10(主)
- 提交7
- commit9 v1.73.0
- 提交8
- commit6 v1.72.0
- 提交5
- commit4 v1.71.0
- 提交3
- 提交2
- 提交1
回答by torek
How tags work
标签的工作原理
In git, each tag is said to "point to" a (one, single) commit. In fact, the same is true of a branch: a branch name alsojust points to one commit.
在 git 中,每个标签都被称为“指向”一个(一个,单个)提交。事实上,分支也是如此:分支名称也只是指向一个提交。
What makes this work is two things:
使这项工作起作用的是两件事:
- each commit also points to another commit (or perhaps several), and
- for branches (and onlyfor branches), the commit to which the branch points "moves forward" automatically. That is, as you add new commits—in some ways, that's mostly all git does: add new commits to its collective, sort of like the Borg from the old Star Trek TNG series—whatever branch you're on, that's the branch that gets re-adjusted to point to the new commit.
- 每个提交也指向另一个提交(或者可能是几个),并且
- 对于分支(并且仅针对分支),分支指向的提交会自动“向前移动”。也就是说,当你添加新的提交时——在某些方面,这就是 git 所做的大部分事情:向它的集合添加新的提交,有点像旧星际迷航 TNG 系列中的 Borg——无论你在哪个分支上,那都是那个分支重新调整以指向新的提交。
Thus, the main difference between a branch and a tag is that a tag does not move.
因此,分支和标签之间的主要区别在于标签不会移动。
To see how this works, consider a simple git repository with just three commits. Let's label these commits A
, B
, and C
. The first commit (A
) points to nothing, as it's the first commit, and branch master
points to A
:
要了解这是如何工作的,请考虑一个只有三个提交的简单 git 存储库。让我们来标注这些提交A
,B
和C
。第一次提交 ( A
) 不指向任何内容,因为它是第一次提交,而分支master
指向A
:
A <-- master
When you make the second commit, git creates B
pointing back to A
, and advances the branch name to point to B
:
当您进行第二次提交时,git 创建B
指向A
,并将分支名称推进到指向B
:
A <- B <-- master
Then when you make a third commit, git again makes it point back to its parent commit, and advances the branch:
然后当你进行第三次提交时,git 再次让它指向它的父提交,并推进分支:
A <- B <- C <-- master
If you make a tag now, that tag will, by default, point to commit C
:
如果你现在创建一个标签,默认情况下,该标签将指向 commit C
:
A <- B <- C <-- master
^
|
tag: sometag
If you then make a new commit D
, git advances the branch, but not the tag:
如果您随后进行新的提交D
,git 会推进分支,但不会推进标签:
A <- B <- C <- D <-- master
^
|
tag: sometag
You can, at any time, create or delete any tag pointing to any particular commit:
您可以随时创建或删除指向任何特定提交的任何标签:
$ git tag -d sometag
will delete tag sometag
, after which:
将删除 tag sometag
,之后:
$ git tag sometag master~2
will add sometag
pointing to commit B
.1
将添加sometag
指向 commit B
。1
(We've just proven that a tag canmove. The real difference is that tags are not expectedto move, while branches are; and git won't move tags automatically.2Branches are generally expected to move in a "forward" direction, i.e., if master
used to point to commit C
and now points to commit D
, commit C
should usually be found by starting at D
and working backwards. Any time you move a branch so that this rule is violated, you're "rewriting history"; see other articles for when this is fine, and when it causes people trouble.)
(我们刚刚证明,一个标签可以移动的真正区别是标签不。预计移动,而分支;和Git不会自动将标签2个分行的普遍预期,“向前”的方向移动, 即, 如果master
以前指向 commitC
而现在指向 commit D
,C
通常应该通过从 at 开始D
并向后工作来找到提交. 任何时候移动分支以违反此规则, 就是在“重写历史记录”;参见其他什么时候这很好,什么时候会给人们带来麻烦的文章。)
Pushing tags
推送标签
When you use git push
, what you're really doing is instructing some other git repository to take any new commits you have that they don't, and then set some name(s)—usually branches and/or tags—to point to some commits (one each) in the resulting collection.3These names (branches, tags, and so on) are called "references" in general, but let's just use "branch" and "tag" for now.
当您使用 时git push
,您真正在做的是指示其他一些 git 存储库接受您没有的任何新提交,然后设置一些名称(通常是分支和/或标签)以指向一些提交(每个)在结果集合中。3这些名称(分支、标签等)通常称为“引用”,但现在我们只使用“分支”和“标签”。
The argument after git push
names the repository (generally via a "remote" name, like origin
) to push-to. If you leave it out, git will try to figure one out, but if you want to add a branch or tag name, you need to include it explicitly, since the first word here is assumed to be the remote-name. (That is, git push master
tries to use master
as a remote-name rather than a branch-name.)
将git push
存储库命名origin
为 push-to之后的参数(通常通过“远程”名称,如)。如果你忽略它,git 会尝试找出一个,但如果你想添加一个分支或标签名称,你需要明确地包含它,因为这里的第一个词被假定为远程名称。(也就是说,git push master
尝试master
用作远程名称而不是分支名称。)
To push allyour tags, you can simply add --tags
to your git push
command:
要推送所有标签,您只需添加--tags
到您的git push
命令中:
git push --tags origin
To push a specifictag, you can name it:
要推送特定标签,您可以将其命名为:
git push origin sometag
just as you can push a specific branch:
就像您可以推送特定分支一样:
git push origin master
(In fact, that fourth argument is a pairof names, like master:master
or sometag:sometag
, but it defaults to using the same name on both sides in most cases.4)
(实际上,第四个参数是一对名称,例如master:master
or sometag:sometag
,但在大多数情况下,它默认在两侧使用相同的名称。4)
You can leave out the name origin
if you don't need it to make all the arguments, e.g., git push --tags
is the same as git push --tags origin
(assuming all your pushes go to origin
, anyway).
origin
如果您不需要它来生成所有参数,您可以省略名称,例如,git push --tags
is 相同git push --tags origin
(假设您的所有推送都转到origin
)。
Putting it together
把它放在一起
To set a tag in the remote, first set it locally, with git tag namecommit-identifier
. Use whatever viewer you like to make sure it's set correctly. Then push it, with either git push origin name
or git push --tags
.
要在远程设置标签,首先在本地设置它,使用. 使用您喜欢的任何查看器以确保其设置正确。然后用或推动它。git tag namecommit-identifier
git push origin name
git push --tags
1The master~2
syntax instructs git to start at the commit found via master
, then back up two steps. You could instead write the raw SHA-1 for commit B
here.
1所的master~2
语法指示的git开始在提交经由发现master
,然后备份两个步骤。您可以改为在B
此处编写用于提交的原始 SHA-1 。
2Old versions of git (pre 1.8.4) accidentally applied branch rules to tags when pushing (on the remote side, i.e., they let a tag move if it was a "fast forward").
2旧版本的 git(1.8.4 之前)在推送时不小心将分支规则应用于标签(在远程端,即,如果标签是“快进”,它们会让标签移动)。
3In some cases, you can point a name to an "annotated tag", and there's nothing preventing a name from pointing at a "tree" or "blob" object either, but that's not a normal setup.
3在某些情况下,您可以将名称指向“带注释的标签”,并且没有什么可以阻止名称指向“树”或“blob”对象,但这不是正常设置。
4Actually the default dstrefspec for a branch is complicated: it depends on your push.default
configuration, and whether there is a remote.repository.push
setting, and whether there is an upstream configured, and so on. For tags, the rules are simpler since there's no such thing as an "upstream".
4其实一个分支的默认dstrefspec很复杂:取决于你的push.default
配置,有没有设置,有没有配置upstream等等。对于标签,规则更简单,因为没有“上游”这样的东西。remote.repository.push
回答by qed
Here is a concrete example:
下面是一个具体的例子:
git add .
git commit -m "some description"
git tag v0.1.9 # or any other text
git push origin master # push the commit
git push --tags origin # push the tags
回答by Chris
Once you've created the tag (which it looks like you've done), simply run
一旦你创建了标签(看起来你已经完成了),只需运行
git push --tags origin