How can I archive git branches?

后端 未结 11 2170
無奈伤痛
無奈伤痛 2020-11-27 09:00

I have some old branches in my git repository that are no longer under active development. I would like to archive the branches so that they don\'t show up by default when r

11条回答
  •  情深已故
    2020-11-27 09:24

    Yes, you can create a ref with some non-standard prefix using git update-ref. e.g.

    • Archive the branch: git update-ref refs/archive/old-topic topic && git branch -D topic
    • Restore the branch (if needed): git branch topic refs/archive/old-topic

    Refs with non-standard prefix (here refs/archive) won't show up on usual git branch, git log nor git tag. Still, you can list them with git for-each-ref.

    I'm using following aliases:

    [alias]
        add-archive = "!git update-ref refs/archive/$(date '+%Y%m%d-%s')"
        list-archive = for-each-ref --sort=-authordate --format='%(refname) %(objectname:short) %(contents:subject)' refs/archive/
        rem = !git add-archive
        lsrem = !git list-archive
    

    Also, you may want to configure remotes like push = +refs/archive/*:refs/archive/* to push archived branches automatically (or just specify on push like git push origin refs/archive/*:refs/archive/* for one-shot ).

    Another way is to writing down SHA1 somewhere before deleting branch, but it has limitations. Commits without any ref will be GC'd after 3 months (or a couple of weeks without reflog), let alone manual git gc --prune. Commits pointed by refs are safe from GC.

    Edit: Found a perl implementation of the same idea by @ap: git-attic

    Edit^2: Found a blog post where Gitster himself using the same technique. He named it git hold.

提交回复
热议问题