Git branch deletion

前端 未结 4 2151
情歌与酒
情歌与酒 2021-01-29 19:22

In Git, what does \"deletion\" of a branch mean?

Will it be gone from the repository? Or will it still be navigable to via git branch?

What I reall

4条回答
  •  误落风尘
    2021-01-29 20:07

    You can delete the branch, but tag it first, so that it's history doesn't disappear. This way, the branch doesn't show up in the branch list, which should hopefully deter people from working on it, but the work won't be permanently erased (even after garbage collection is run). For instance, whenever I have a branch that has become irrelevant, but I'm not prepared to permanently delete it, I tag it as "archive/<branch-name>".

    While on master or some other branch:

    git tag archive/foo foo
    git branch -D foo
    

    This creates a tag named archive/foo from the foo branch before deleting foo. You can also add a message to the tag, that explains what is in the branch, why it existed, why it's now a dead end, etc.

    git tag -m 'Foo is deprecated in favor of bar' archive/foo foo
    

    The ability to record why a branch is being deprecated is perhaps an advantage of tagging versus moving branches to an alternate namespace.

    If you ever need to resurrect a branch that has been archived this way, it's as simple as:

    git branch foo archive/foo
    git tag -d archive/foo       # Optional
    

    Now the branch is back as though it was never deleted.

提交回复
热议问题