git查看打tag时间_更改git标签的日期(或基于它的GitHub发布)

本文介绍如何修复GitHub上项目标签日期错误,确保与实际commit时间对应。涉及删除并重新添加带有正确创建日期的标签,保持版本历史有序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I'm adding Releases to my projects on GitHub by adding tags to various commits in the Main branch.

In one of my projects I did not add the tags to the commits in chronological order. (I found obvious commits and tagged them, and then I found less obvious, older commits and tagged them.)

Now GitHub is showing v1.0.1 as current, with v0.7.0 preceding it, and v1.1.2 preceding that.

It appears to use the date on a tag's creation as the Release date instead of the commit that is being tagged. How can I edit my tags so that their dates are the same as the commit they are tagging?

解决方案WARNING: This will not preserve tag messages for annotated tags.

Summary

For each tag that needs to be changed:

Go back in time to the commit representing the tag

Delete the tag (locally and remotely)

This will turn your "Release" on GitHub into a Draft that you can later delete.

Re-add the same-named tag using a magic invocation that sets its date to the date of the commit.

Push the new tags with fixed dates back up to GitHub.

Go to GitHub, delete any now-draft releases, and re-create new releases from the new tags

In code:

# Fixing tag named '1.0.1'

git checkout 1.0.1 # Go to the associated commit

git tag -d 1.0.1 # Locally delete the tag

git push origin :refs/tags/1.0.1 # Push this deletion up to GitHub

# Create the tag, with a date derived from the current head

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

git push --tags # Send the fixed tags to GitHub

Details

If you forget to tag a release or version bump, you can always tag it retroactively like so:

git checkout SHA1_OF_PAST_COMMIT

git tag -m"Retroactively tagging version 1.5" v1.5

And while that's perfectly usable, it has the effect of putting your tags out of chronological order which can screw with build systems that look for the "latest" tag. But have no fear. Linus thought of everything:

# This moves you to the point in history where the commit exists

git checkout SHA1_OF_PAST_COMMIT

# This command gives you the datetime of the commit you're standing on

git show --format=%aD | head -1

# And this temporarily sets git tag's clock back to the date you copy/pasted in from above

GIT_COMMITTER_DATE="Thu Nov 11 12:21:57 2010 -0800" git tag -a 0.9.33 -m"Retroactively tagging version 0.9.33"

# Combining the two...

GIT_COMMITTER_DATE="$(git show --format=%aD | head -1)" git tag -a 0.9.33 -m"Retroactively tagging version 0.9.33"

However, if you have already added the tag, you cannot use the above with git tag -f existingtag or else git will complain when you try to merge:

Rammy:docubot phrogz$ git push --tags

To git@github.com:Phrogz/docubot.git

! [rejected] 1.0.1 -> 1.0.1 (already exists)

error: failed to push some refs to 'git@github.com:Phrogz/docubot.git'

hint: Updates were rejected because the tag already exists in the remote.

Instead, you must remove the tag locally:

git tag -d 1.0.1

git push origin :refs/tags/1.0.1

On GitHub, reload Releases—the release has now been marked as a "Draft"—and remove the draft.

Now, add the backdated tag based on the instructions above, and finally push the resulting tag to GitHub:

git push --tags

and then go and re-add the GitHub Release information again.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值