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

发布于 2024-10-07 12:43:19 字数 117 浏览 5 评论 0 原文

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

那么如何向任意较旧的提交添加标签呢?

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

浊酒尽余欢 2024-10-14 12:43:19

示例:

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

其中 9fceb02 是提交 ID 的开始部分。

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

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

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

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

Example:

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

Where 9fceb02 is the beginning part of the commit id.

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

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

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

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.

○愚か者の日 2024-10-14 12:43:19

只是代码

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

# push to origin
git push origin --tags

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

详细信息

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

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

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

例如:

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

页面"如何在 Git 中标记"向我们展示了我们可以通过以下方式提取 HEAD 提交的时间:

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

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

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

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

git checkout 9fceb02 

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

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"

# push to origin
git push origin --tags

# 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 tag command), not the date of the commit. The Git help for tag has a section "On Backdating Tags" which says:

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

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

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:

git checkout 9fceb02 

GIT_COMMITTER_DATE="$(git show --format=%aD | head -1)" git tag -a v1.2 -m "v1.2"
辞取 2024-10-14 12:43:19

最简单的方法是:

git tag v1.0.0 f4ba1fc
git push origin --tags

f4ba1fc 作为要标记的提交的哈希的开头,v1.0.0 是要标记的版本。

The simplest way to do this is:

git tag v1.0.0 f4ba1fc
git push origin --tags

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

木槿暧夏七纪年 2024-10-14 12:43:19

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

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

因此,如果您想添加 标签: 1.0.2 来提交 e50f795,只需执行以下操作:

git tag -a 1.0.2 e50f795

您也 <强>在最后添加一条消息,使用-m,如下所示:

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

毕竟,您需要将其推送到远程,才能做到这一点,简单地做:

git push origin 1.0.2 

如果你有很多标签,你不想一一提及它们,只需简单地做:

git push origin --tags

将所有标签放在一起......

另外,我创建了下图中的步骤,以更清楚地说明步骤:
正在创建标签提交哈希

您也可以在Hub中添加标签或使用SourceTree等工具,为了避免前面的步骤,我在这个中登录了我的Bitbucket案例并从那里执行操作:

  1. 转到您的分支并找到要添加标签的提交,然后单击它:

在 bitbucket 中查找您的提交

  1. 提交页面< /strong>,在右侧找到无标签的位置,然后单击+图标:

查找显示无标签的位置

  1. 在标签名称框中,添加您的标签:

add tag name

  1. 现在您看到标签已成功创建:

在此处输入图像描述

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:

git tag -a 1.0.2 e50f795

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

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:

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 Hub or using tools like SourceTree, to avoid the previous steps, I logged-in to my Bitbucket in this case and doing it from there:

  1. Go to your branch and find the commit you want to add the tag to and click on it:

find your commit in bitbucket

  1. In the commit page, on the right, find where it says No tags and click on the + icon:

find where it says No tags

  1. In the tag name box, add your tag:

add tag name

  1. Now you see that the tag has successfully created:

enter image description here

今天小雨转甜 2024-10-14 12:43:19

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

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

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.

机场等船 2024-10-14 12:43:19

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

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

The answer by @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:

git checkout 9fceb02
$env:GIT_COMMITTER_DATE = git show --format=%aD | Select -First 1
git tag v1.2
git checkout master
习ぎ惯性依靠 2024-10-14 12:43:19

基于其他人的答案,这里是一个单行解决方案,它将标记日期设置为实际发生的时间,使用带注释的标记并且不需要 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

其中 tag 设置为所需的标记字符串,并将 commit 设置为提交哈希。

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:

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 tag is set to the desired tag string, and commit to the commit hash.

病女 2024-10-14 12:43:19

要标记特定提交,首先打印提交哈希值以查看要为其添加标签的提交

git log --oneline

输出如下:

dee93fc update App.js
c691fa2 autherization to roles
559528a modify depart
6aa4ad4 edit project page

选择要为其添加标签的提交 ID,然后 git checkout 获取提交 ID

git checkout 6aa4ad4

并为其添加标签提交

git tag v1.0

并返回到你的分支 制作此标签后

git checkout branchName

查看所有标签

git tag

To tag a specific commit, print commits hashes first to view what commit that you want to add tag to it

git log --oneline

The output would like this:

dee93fc update App.js
c691fa2 autherization to roles
559528a modify depart
6aa4ad4 edit project page

Select commit id that you want to add tag to it and git checkout for commit id

git checkout 6aa4ad4

And add tag for that commit

git tag v1.0

And return to your branch After make this tag

git checkout branchName

To view all tags

git tag
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文