Home > Enterprise >  Delete tags fetched from one repository of many
Delete tags fetched from one repository of many

Time:08-19

I have a ported open source project and I want to update it on new version, so I added remote repository which contain necessary updates of project, but I've mistaken with link and add another one, execute git fetch --all so I fetch all commits and tags from remote repository to my local repo, after that add correct remote repo and fetched it, resolve merge conflict and push if with tags git push --tags, so I got tags from two repositories.

How can I delete tags from the first remote repository, which was wrong?

CodePudding user response:

First, a note: git fetch --all does not fetch all branches, nor all tags. It fetches the same branches and/or tags it always would fetch. What --all means here is all remotes. That is, for each remote printed by git remote or git remote -v, it will run one git fetch to that remote. So this has nothing to do with the actual problem, which is simply that you set up a remote-and-URL that you hadn't intended, and fetched from that one.

To delete some particular set of tags from your repository, use git tag -d. This command allows you to specify more than one tag:

git tag -d tag1 tag2 tag3

will delete all three tags.

To ask some other repository to delete its tags, use git push with --delete or the refspec syntax that means "delete". That is, suppose you want to ask origin to delete its tags tag1, tag2, and tag3. Then you want:

git push --delete origin refs/tags/tag1 refs/tags/tag2 refs/tags/tag3

or:

git push origin :refs/tags/tag1 :refs/tags/tag2 :refs/tags/tag3

You can leave out the refs/tags/ in each position, but this runs the risk of deleting a branch named tag2, for instance; using refs/tags/tag2 makes it explicit that you would like origin to delete its tag tag2.

@phd Yes, the question is how to delete tags whose were fetched from wrong repository. And do it easy, not by deleting all wrong tags one by one.

You still must delete each tag individually. You can do that with one command, but that command has to list each tag to be deleted.

To get a list of tags you'd like deleted, consider asking the remote that had the unwanted tags for a list of its tags:

git ls-remote --tags <name-or-URL>

You'll probably want to redirect this output to a file. Open the file in a text-file editor, remove all the lines that end with ^{}, and remove the hash ID and initial refs/tags/ parts that remain. In vim, for instance:

vim /tmp/tags
:g/\^{}$/d
:%s,.*\trefs\/tags\/,,

will do the trick. You now have a list of all the tags that are in that repository.

Note that blindly assuming that those tags should be deleted might be wrong! Some of those tags may have already existed and still name the correct commits in your repository. So you must now inspect these tags and make sure you really want to delete all of them.

Once you have the list, you can use xargs (if you have that), or your text editor again, to build the appropriate git tag -d and/or git push --delete arguments. For instance:

sed 's,.*,git tag -d &,' < /tmp/tags

produces a full list of git tag -d commands; pipe this into the shell to make them execute:

sed 's,.*,git tag -d &,' < /tmp/tags | sh

Repeat with a substitute that builds a git push --delete origin refs/tags/& command from the tag name. (When and whether to optimize this into a single git push is up to the user.)

  • Related