Reputation: 35
So currently I have a simple Jenkins job that fetches origin, creates a tag, and pushes the tag. This Jenkins job is run once a week and the date ran is part of the tag name. 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). This done in around 10 repos, but my issue only happens in one repo.
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). 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). This repo * should * be the same as the other 9 repos that do not have this issue.
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):
TODAY=$(date +'%Y%m%d)
git fetch origin && git tag DIFFS_$TODAY && git push origin DIFFS_$TODAY
Thanks!
Upvotes: 0
Views: 845
Reputation: 114987
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.
When you perform git pull
, the latest commits are fetch
ed and then git will try to advance to the latest version. Depending on the state of your local repo & config it may do one of the following:
Each of these can fail, hence pull
is a bit of a dangerous option. A safer option, if you know you want exactly the changes on origin, is to fetch
, then 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
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
Upvotes: 2