48

How you I change the connected commit on a github release? Release

I want to change it to an earlier commit, because I created the release afterwards (after some commits to release 0.9)

Zoker
  • 2,020
  • 5
  • 32
  • 53

1 Answers1

61

When you consider the GitHub API for creating a release, you see a release needs:

  • a tag
  • a commit referenced by that tag

So you need to move your tag (locally first, then push it to your GitHub repo)

git tag -f -a <tagname> [<commit> | <object>]
# or, to avoid an editor
git tag -m "tag message" -f -a <tagname> [<commit> | <object>]

git push -f <remotename> refs/tags/<tagname>

for Example:

git tag -m "moving tag to new commit" -f -a my_tag [commit hash]
git push -f origin refs/tags/my_tag

Then see if that is enough for the release to be updated.
(See "How do you push a tag to a remote repository using Git?")

If not, you might have to delete that release, and recreate it on the same tag (which will refer to the new commit)


After doing this, if you get an error would clobber existing tag, then you need to sync local tags with remote using git fetch --tags -f and the push new commits.

Gangula
  • 5,193
  • 4
  • 30
  • 59
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    I did the first line before posting here, but it did not work. The second line fixed it :) – Zoker Jul 20 '14 at 13:33
  • how do you specify the commit id in the first command? My commit id is: `10a4ff2`. So what should my command be? `git tag -f -a v1.2.0 ...???...` – Parth Jan 29 '21 at 07:48
  • 1
    @ParthTamane Simply `git tag -f -a v1.2.0 10a4ff2`, as illustrated in https://git-scm.com/book/en/v2/Git-Basics-Tagging#_tagging_later – VonC Jan 29 '21 at 07:52
  • @Sam Sure, I have edited the answer accordingly. You can also edit it directly: I will review and approve your edit. – VonC Feb 12 '21 at 19:25
  • Note that once you run `git tag -f -a `, you'll be prompted to enter a messgae in a texteditor. You need to type a mesage and save the file to proceed. Without the message you'll get `fatal: no tag message?` error – Gangula Sep 20 '22 at 05:27
  • 1
    @Gangula Running with -m (`git tag -m "tag message" -f -a [ | ]`) should be enough to avoid the editor part. – VonC Sep 20 '22 at 06:41
  • after doing this I got an error when I try to push a new commit - `would clobber existing tag`. So before pushing any new commits I had to fetch tags from remote using `git fetch --tags -f`. Reference: https://stackoverflow.com/a/58438257/6908282 – Gangula Sep 20 '22 at 09:25
  • @Gangula Good point. Thank you for the edit! – VonC Sep 20 '22 at 11:42