How can I archive git branches?

后端 未结 11 2018
無奈伤痛
無奈伤痛 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:33

    I am using following aliases to hide archived branches:

    [alias]
        br = branch --no-merge master # show only branches not merged into master
        bra = branch                  # show all branches
    

    So git br to show actively developed branches and git bra to show all branches including "archived" ones.

    0 讨论(0)
  • 2020-11-27 09:33

    My approach is to rename all branches I don't care about with a "trash_" prefix, then use:

    git branch | grep -v trash
    

    (with a shell key binding)

    To retain the coloring of the active branch, one would need:

    git branch --color=always | grep --color=never --invert-match trash
    
    0 讨论(0)
  • 2020-11-27 09:36

    I believe the proper way to do this is to tag the branch. If you delete the branch after you have tagged it then you've effectively kept the branch around but it won't clutter your branch list.

    If you need to go back to the branch just check out the tag. It will effectively restore the branch from the tag.

    To archive and delete the branch:

    git tag archive/<branchname> <branchname>
    git branch -d <branchname>
    

    To restore the branch some time later:

    git checkout -b <branchname> archive/<branchname>
    

    The history of the branch will be preserved exactly as it was when you tagged it.

    0 讨论(0)
  • 2020-11-27 09:39

    Extending Steve's answer to reflect the changes on the remote, I did

     git tag archive/<branchname> <branchname>
     git branch -D <branchname>
     git branch -d -r origin/<branchname>
     git push --tags
     git push origin :<branchname>
    

    To restore from the remote, see this question.

    0 讨论(0)
  • 2020-11-27 09:43

    Jeremy's answer is correct in principle, but IMHO the commands he specifies are not quite right.

    Here's how to archive a branch to a tag without having to checkout the branch (and, therefore, without having to checkout to another branch before you can delete that branch):

    > git tag archive/<branchname> <branchname>
    > git branch -D <branchname>
    

    And here's how to restore a branch:

    > git checkout -b <branchname> archive/<branchname>
    
    0 讨论(0)
提交回复
热议问题