简体   繁体   English

Git 标记指向错误的提交 ID

[英]Git Tag Points to Wrong Commit ID

So currently I have a simple Jenkins job that fetches origin, creates a tag, and pushes the tag.所以目前我有一个简单的 Jenkins 作业,它获取来源、创建标签并推送标签。 This Jenkins job is run once a week and the date ran is part of the tag name.这个 Jenkins 作业每周运行一次,运行日期是标签名称的一部分。 I do these weekly tags to make git patches and merge different baselines together if desired for core changes (all this stuff happens in separate Jenkins jobs and I doubt is relevant).我做这些每周标记来制作 git 补丁,如果需要进行核心更改,则将不同的基线合并在一起(所有这些东西发生在单独的 Jenkins 作业中,我怀疑是否相关)。 This done in around 10 repos, but my issue only happens in one repo.这是在大约 10 个回购中完成的,但我的问题只发生在一个回购中。

In the repo with the issue, this job has now been running for roughly 3 years and has around 160 tags of these tags (around 380 tags total in the repo).在有问题的回购中,这项工作现在已经运行了大约 3 年,并且有大约 160 个这些标签的标签(回购中总共有大约 380 个标签)。 Recently, about once every 3-4 months (4 times in 2020 from September to December), there have been issues with the tags where they point to a commit from 3.75 years ago (every messed up tag points to the same commit ID).最近,大约每 3-4 个月一次(2020 年从 9 月到 12 月有 4 次),它们指向 3.75 年前的提交的标签出现问题(每个混乱的标签都指向相同的提交 ID)。 This repo * should * be the same as the other 9 repos that do not have this issue.这个回购 *应该* 与其他 9 个没有这个问题的回购相同。

I cannot replicate this as it does not happen often but any ideas as to why this might be happening or ways to prevent it from happening again please let me know!我无法复制它,因为它不会经常发生,但是关于为什么会发生这种情况或防止它再次发生的方法的任何想法请告诉我!

Shell code from the Jenkins job (git calls repeat in loop for each repo):来自 Jenkins 作业的 Shell 代码(git 为每个 repo 循环调用 repeat):

TODAY=$(date +'%Y%m%d)

git fetch origin && git tag DIFFS_$TODAY && git push origin DIFFS_$TODAY

Thanks!谢谢!

When you perform git fetch , the new commits on the remote are added to your local repo, but the HEAD isn't advanced to that latest commit, it still points to the commit the repo was looking at when the last pull/reset/commit has taken place.当您执行git fetch时,远程上的新提交将添加到您的本地存储库中,但HEAD不会提前到最新的提交,它仍然指向存储库在上次拉取/重置/提交时正在查看的提交发生了。

When you perform git pull , the latest commits are fetch ed and then git will try to advance to the latest version.当您执行git pull时,最新的提交被fetch编辑,然后 git 将尝试推进到最新版本。 Depending on the state of your local repo & config it may do one of the following:根据您本地存储库和配置的 state,它可能会执行以下操作之一:

  • fast-forward快进
  • merge合并
  • rebase变基

Each of these can fail, hence pull is a bit of a dangerous option.这些中的每一个都可能失败,因此pull是一个有点危险的选择。 A safer option, if you know you want exactly the changes on origin, is to fetch , then reset --hard .一个更安全的选择,如果你知道你想要 origin 上的确切更改,是fetch ,然后reset --hard That will not try to do any merges and forces a fast-forward.这不会尝试进行任何合并并强制快进。

If all you want, is to set a tag, you don't even need the local working directory to reflect the remote changes.如果你只想设置一个标签,你甚至不需要本地工作目录来反映远程更改。 After fetching you can specify you want to tag origin/main获取后你可以指定你想要tag origin/main

 git fetch origin/main
 git tag DIFFS_$TODAY origin/main
 git push --tags DIFFS_$TODAY

But there is an even simpler way (or at least faster), you can push the tag without having to create it locally, but you still have to fetch the changes:但是有一种更简单的方法(或者至少更快),你可以推送标签而不必在本地创建它,但你仍然需要获取更改:

 git push origin origin/main:refs/tags/DIFFS_$TODAY

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM