3

How to edit tag in github, without cloning the repo from command line?

Example below shows how to list remote tags, without cloning the repo:

 $ git ls-remote --tags https://<TOKEN>@github.com/user/repo.git
 # 0afdaf971...09a  refs/tags/tagname

How to remotely edit/add/del new tag like example above without having to clone the repo?

git push origin :tagname

Requires of cloning the repo.

Is there a way to achieve it without cloning repo?

Mike S.
  • 205
  • 2
  • 12
  • Possible duplicate of [Create a tag in a GitHub repository](https://stackoverflow.com/questions/18216991/create-a-tag-in-a-github-repository) – xerx593 Feb 05 '19 at 22:25
  • ( https://stackoverflow.com/a/18223354/592355 -> Creating tags through GitHub's web interface) – xerx593 Feb 05 '19 at 22:26
  • Thanks for the answer, but i need it from command line. – Mike S. Feb 05 '19 at 22:35
  • 1
    ..then you *must* clone! (what do you want to tag otherwise? ..a (non) empty folder/repository??) ..of course the work around: "call github from cmd" (with curl, when all else fails)) – xerx593 Feb 05 '19 at 22:36
  • there is remote repository, I am looking for solution to remotely update the tag, to trigger builds. – Mike S. Feb 05 '19 at 22:37
  • @xerx593 I think that could be a solution! could you give example plz! – Mike S. Feb 05 '19 at 22:39
  • ..we have to elaborate, but starting: [here](https://developer.github.com/v3/repos/releases/#create-a-release) (github rest api doc! section: create a release :) – xerx593 Feb 05 '19 at 22:42
  • @xerx593 Thanks for the guidance! found solution. – Mike S. Feb 05 '19 at 23:00

3 Answers3

2

@xerx593

Thank you for pointing to the right direction, here is solution:

curl --user "username:password" --data '{"tag_name":"v1.1","target_commitish":"master"}' \
-X POST https://api.github.com/repos/:owner/:repo/releases
Mike S.
  • 205
  • 2
  • 12
0

I figured out how to do this in a generic way in git (this may break in the future).

The following variables are used:

BRANCH_NAME # I think this can be anything
REPOSITORY_URL
COMMIT_HASH # The commit to tag
TAG_NAME # The name of the tag

For git push to work, you need to have a .git directory created as follows:

mkdir -p .git/objects .git/refs
echo 'ref: refs/heads/$BRANCH_NAME' > .git/HEAD
printf '[remote "origin"]\n    url = $REPOSITORY_URL'

Additionally, you need to have a "commit" object in the objects directory. This file has a bit of information that's difficult to transfer over and re-create. It's easier to just copy the file over (it's a small file). The following will get the file:

mkdir -p output_directory/objects/${COMMIT_HASH:0:2}/
cp .git/objects/.git/objects/${COMMIT_HASH:0:2}/${COMMIT_HASH:2} output_directory/objects/${COMMIT_HASH:0:2}/${COMMIT_HASH:2}

And then when you're ready, you can copy this file to the new stub git repository in the .git/objects/${COMMIT_HASH:0:2}/${COMMIT_HASH:2} directory.

Then in the new stub git directory, push the new tag like this:

git push +${COMMIT_HASH}:refs/tags/${TAG_NAME}

Remember to set up your git credentials. This can be done as follows:

git config credential.helper store
echo "https://$GIT_USERNAME:$GIT_PASSWORD@$GIT_REPOSITORY_DOMAIN" >  ~/.git-credentials
Tim Ludwinski
  • 2,704
  • 30
  • 34
0

Found this too. If the commit you want to tag is the latest of a particular branch, you can download only that commit like this:

git clone --bare --single-branch --branch $BRANCH_NAME --depth=1 --filter=tree:0 --filter=blob:none --filter=object:type=commit $REPOSITORY_URL .git

This requires a later version of git which supports --filter and --filter=object:type=commit. Besides the commits, this seems to also download the hooks directory, which on my repository is 40k (I think this is with an empty list of hooks).

Then you can tag it as usual:

git push origin +$BRANCH_NAME:refs/tags/${TAG_NAME}
Tim Ludwinski
  • 2,704
  • 30
  • 34