Home > Software design >  GitHub tag not working correctly "This commit does not belong to any branch on this repository,
GitHub tag not working correctly "This commit does not belong to any branch on this repository,

Time:06-16

I'm setting up a task in our azure pipeline to:

  • bump the project version number in version.txt
  • update the CHANGELOG
  • commit those two changes
  • create a tag with the new version number
  • push changes back to github

This is happening on our main branch.

My script looks like this:

echo "$new_version" > version.txt
git-changelog --tag "$new_version"
git add CHANGELOG
git add version.txt
git commit -m "Bump to release $new_version"
git tag -a "$new_version" -m "release $new_version"
git push origin "$new_version"

When I look back in GitHub, the updated CHANGELOG and version.txt don't appear in main. However, the new tag exists. When I click to view the tag, I can see the two file changes in that commit. But I get a warning message on screen saying:

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

GitHub Warning: This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Not sure what I'm doing wrong, and how to get the commit to apply to our main branch. The code checkout is being done by azure pipelines before my script runs, do I need to explicitly switch / checkout branches before running my commands?

Update

Azure pipelines checkout performs a checkout of the commit, not the branch. I needed to update my script to perform a checkout of main and then perform my stage and commits to that branch, and then push and tag worked as I was hoping.

git checkout main
#
# work to modify CHANGELOG and version.txt here...
#
git status -v -b
git add CHANGELOG
git add version.txt
git commit -m "Bump to release $new_version"
git push origin main
git tag "$new_version"
git push origin "$new_version"

CodePudding user response:

You are pushing the tag, not the branch. Push the branch.

In other words, you are complaining that this commit is not on main on GitHub, but you are not saying anything that would cause it to be on main on GitHub.

do I need to explicitly switch / checkout branches before running my commands?

Maybe. There's nothing in your script that tells us whether you were on main when you committed. But even if you were, main is not what you pushed, and main is not what you pushed to.

If you are detached (i.e. not on any branch), it is still possible to push the tag to main. But the syntax for that is tricky because this is an annotated tag. See How do you push a Git tag to a branch using a refspec? for more.

  • Related