Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I archive old git tags?

Tags:

git

I have some old tags in my git repository that are no longer important. I would like to archive the tags so that they don't show up by default when running git tag. I don't want to delete them, because I want to keep the history. How can I do this?

like image 786
Dan Fabulich Avatar asked Nov 26 '14 20:11

Dan Fabulich


People also ask

How do I remove old tags in git?

To delete the Git tag from the local repo, run the git tag -d tag-name command where tag-name is the name of the Git tag you want to delete. To get a list of Git tag names, run git tag.

Can you reuse git tags?

Git tags can't be reused. A tag can be removed and reassigned.


2 Answers

It's possible to keep the tags in the repo and avoid listing them as tags. It also avoids default clones from fetching them.

In origin (the bare repo) assuming you want to archive all tags starting with 'HIDE':

mkdir refs/archive
mv refs/tags/HIDE* refs/archive/

Now git tag will no longer list these refs, however they will still exist in the repository.

If you later want to pull down the archived tags to your local clone you can run the following:

git fetch origin +refs/archive/*:refs/archive/*

You can now list them using:

git for-each-ref refs/archive
like image 133
Zitrax Avatar answered Sep 25 '22 07:09

Zitrax


You should be careful when you delete tags. Maybe a tag is the only reference to some sommit.

                      branch
                      |
                      V
 o---o---o----o---o---o
     \
      o---o---o---o
                  ^
                  |
                  someTag

If you remove the last reference to a commit you will loose the commit someday. Someday means as soon as git gc collects them.

If you don't have this problems you can use

$ git show-ref --tags > tag_refs-20141126.txt

to dump your tags and the commit ids to to a file. Then commit this file to your repository or keep it somewhere else. After this you can delete the tags

$ git for-each-ref --format="%(refname)" refs/tags | cut -c11- | \
  while read tag; do git tag -d $tag; done

and restore them later

$ cat tag_refs-20141126.txt | while read commitid tagref; do echo $tagref $commitid; done \
  | cut -c11- | while read tagname commitid; do git tag $tagname $commitid; done

EDIT

I would prefer Dan Fabulich or Zitrax's answer. Using hidden refs is what I also do. E.g. when I want to backup stashes. I wrote a git extension for that some time ago.

like image 34
René Link Avatar answered Sep 23 '22 07:09

René Link