如何在 Git 中标记较旧的提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4404172/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:37:52  来源:igfitidea点击:

How to tag an older commit in Git?

gitversion-controlgit-tag

提问by hogsolo

We are new to git, and I want to set a tag at the beginning of our repository. Our production code is the same as the beginning repository, but we've made commits since then. A tag at the beginning would allow us to "roll back" production to a known, stable state.

我们是 git 的新手,我想在我们存储库的开头设置一个标签。我们的生产代码与初始存储库相同,但从那时起我们就进行了提交。开头的标签将允许我们将生产“回滚”到已知的稳定状态。

So how to add a tag to an arbitrary, older commit?

那么如何将标签添加到任意的、较旧的提交中呢?

回答by dkinzer

Example:

例子:

git tag -a v1.2 9fceb02 -m "Message here"

Where 9fceb02is the beginning part of the commit id.

9fceb02提交 id 的开始部分在哪里。

You can then push the tag using git push origin v1.2.

然后,您可以使用 推送标签git push origin v1.2

You can do git logto show all the commit id's in your current branch.

您可以git log显示当前分支中的所有提交 ID。

There is also a good chapter on taggingin the Pro Git book.

在 Pro Git 书中还有一个关于标记的好章节。

Warning:This creates tags with the current date(and that value is what will show on a GitHub releases page, for example). If you want the tag to be dated with the commit date, please look at another answer.

警告:这会创建带有当前日期的标签(例如,该值将显示在 GitHub 发布页面上)。如果您希望标签与提交日期一起注明日期,请查看另一个答案

回答by Phrogz

Just the Code

只是代码

# Set the HEAD to the old commit that we want to tag
git checkout 9fceb02

# temporarily set the date to the date of the HEAD commit, and add the tag
GIT_COMMITTER_DATE="$(git show --format=%aD | head -1)" \
git tag -a v1.2 -m"v1.2"

# set HEAD back to whatever you want it to be
git checkout master

Details

细节

The answer by @dkinzer creates tags whose date is the current date (when you ran the git tagcommand), notthe date of the commit. The Git help for taghas a section "On Backdating Tags"which says:

@dkinzer 的答案创建的标签的日期是当前日期(当您运行git tag命令时),而不是提交日期。Git 帮助tag有一个“关于回溯标签”的部分,它说:

If you have imported some changes from another VCS and would like to add tags for major releases of your work, it is useful to be able to specify the date to embed inside of the tag object; such data in the tag object affects, for example, the ordering of tags in the gitweb interface.

To set the date used in future tag objects, set the environment variable GIT_COMMITTER_DATE(see the later discussion of possible values; the most common form is "YYYY-MM-DD HH:MM").

For example:

$ GIT_COMMITTER_DATE="2006-10-02 10:31" git tag -s v1.0.1

如果您从另一个 VCS 导入了一些更改并希望为您的工作的主要版本添加标签,那么能够指定嵌入标签对象的日期会很有用;例如,标签对象中的此类数据会影响 gitweb 界面中标签的顺序。

要设置未来标记对象中使用的日期,请设置环境变量GIT_COMMITTER_DATE(请参阅后面对可能值的讨论;最常见的形式是“YYYY-MM-DD HH:MM”)。

例如:

$ GIT_COMMITTER_DATE="2006-10-02 10:31" git tag -s v1.0.1

The page "How to Tag in Git"shows us that we can extract the time of the HEAD commit via:

该页面中的“如何在Git的标签”告诉我们,我们可以提取头的时间通过提交:

git show --format=%aD  | head -1
#=> Wed, 12 Feb 2014 12:36:47 -0700

We could extract the date of a specific commit via:

我们可以通过以下方式提取特定提交的日期:

GIT_COMMITTER_DATE="$(git show 9fceb02 --format=%aD | head -1)" \
git tag -a v1.2 9fceb02 -m "v1.2"

However, instead of repeating the commit twice, it seems easier to just change the HEAD to that commit and use it implicitly in both commands:

但是,与其重复提交两次,不如将 HEAD 更改为该提交并在两个命令中隐式使用它似乎更容易:

git checkout 9fceb02 

GIT_COMMITTER_DATE="$(git show --format=%aD | head -1)" git tag -a v1.2 -m "v1.2"

回答by PatrickNLT

The simplest way to do this is:

最简单的方法是:

git tag v1.0.0 f4ba1fc

with f4ba1fcbeing the beginning of the hash of the commit you want to tag and v1.0.0being the version you want to tag.

f4ba1fc是你想要的承诺标签散列的开始,v1.0.0是要标记的版本。

回答by Adrian Cid Almaguer

Use command:

使用命令:

git tag v1.0 ec32d32

Where v1.0 is the tag name and ec32d32 is the commit you want to tag

其中 v1.0 是标签名称,ec32d32 是您要标记的提交

Once done you can push the tags by:

完成后,您可以通过以下方式推送标签:

git push origin --tags

Reference:

参考:

Git (revision control): How can I tag a specific previous commit point in GitHub?

Git(修订控制):如何在 GitHub 中标记特定的先前提交点?

回答by Alireza

OK, You can simply do:

好的,您可以简单地执行以下操作:

git tag -a <tag> <commit-hash>

So if you want to add tag:1.0.2 to commit e50f795, just simply do:

因此,如果您想将tag:1.0.2添加到 commit e50f795,只需执行以下操作:

git tag -a 1.0.2 e50f795

Also you add a messageat the end, using -m, something like this:

您还可以在最后添加一条消息,使用-m,如下所示:

git tag -a 1.0.2 e50f795 -m "my message"

After all, you need to push it to the remote, to do that, simply do:

毕竟,您需要将其推送到remote,为此,只需执行以下操作:

git push origin 1.0.2 

If you have many tags which you don't want to mention them one by one, just simply do:

如果您有很多标签不想一一提及,只需执行以下操作:

git push origin --tags

to push all tags together...

将所有标签推到一起......

Also, I created the steps in the image below, for more clarification of the steps: creating tag on a commit hash

此外,我创建了下图中的步骤,以进一步说明这些步骤: 在提交哈希上创建标签

You can also dd the tag in Hubor using tools like SourceTree, to avoid the previous steps, I logged-in to my Bitbucketin this case and doing it from there:

您还可以在Hub 中添加标签或使用SourceTree 之类的工具,为了避免前面的步骤,在这种情况下,我登录到我的Bitbucket并从那里进行操作:

  1. Go to your branch and find the commityou want to add the tag to and click on it:
  1. 转到您的分支,找到承诺要在标签添加到并单击它:

find your commit in bitbucket

在 bitbucket 中找到您的提交

  1. In the commit page, on the right, find where it says No tagsand click on the +icon:
  1. 提交页面的右侧,找到它所说的位置No tags并单击+图标:

find where it says No tags

找到它说没有标签的地方

  1. In the tag name box, add your tag:
  1. 在标签名称框中,添加您的标签:

add tag name

添加标签名称

  1. Now you see that the tag has successfully created:
  1. 现在您看到标签已成功创建:

enter image description here

在此处输入图片说明

回答by Stephan Bijzitter

This is an old question, and the answers already given all work, but there's also a new option which can be considered.

这是一个老问题,答案已经给出了所有的工作,但还有一个可以考虑的新选项。

If you're using SourceTree to manage your git repositories, you can right-click on any commit and add a tag to it. With another mouseclick you can also send the tag straight to the branch on origin.

如果您使用 SourceTree 来管理您的 git 存储库,您可以右键单击任何提交并向其添加标签。再次单击鼠标,您还可以将标签直接发送到原点的分支。

回答by stason

Building upon the answers of the others, here is a one-liner solution that sets the tag date to when it actually happened, uses annotated tag and requires no git checkout:

基于其他人的答案,这是一个单行解决方案,它将标记日期设置为实际发生的时间,使用带注释的标记并且不需要git checkout

tag="v0.1.3" commit="8f33a878" bash -c 'GIT_COMMITTER_DATE="$(git show --format=%aD $commit)" git tag -a $tag -m $tag $commit'
git push --tags origin master

where tagis set to the desired tag string, and committo the commit hash.

wheretag设置为所需的标记字符串和commit提交哈希。

回答by ccoxtn

The answerby @Phrogz is great, but doesn't work on Windows. Here's how to tag an old commit with the commit's original date using Powershell:

@Phrogz的答案很好,但不适用于 Windows。以下是如何使用 Powershell 用提交的原始日期标记旧提交:

git checkout 9fceb02
$env:GIT_COMMITTER_DATE = git show --format=%aD | Select -First 1
git tag v1.2
git checkout master