我的经验是 Mercurial 对关闭分支机构有很好的支持,举个例子。
Git 可能希望您要么删除分支以保持其可维护性,从而减少分支数,要么坚持使用标签。只要你有一个标签,你就可以再次将它恢复到一个分支中。我使用带有 shell 函数的别名命令,因此我可以轻松关闭一个分支(将其归档为标签以供以后恢复,如果我再次使用它)。
我建议你只关闭你确定以后会有用的分支。在恢复分支并尝试将其合并时,您需要进行大量合并修复的可能性很高。但也可能是您只想查看旧分支中的代码状态,然后将其删除' 但将其作为标签保留。
我在 [alias] 下的 .gitconfig 中使用这种方法
closebranch = "!w() { echo Attempting to close local and remote branch: $1 Processing...; echo Checking the branch $1 out..; git checkout $1; echo Trying to create a new tag archive/$1; git tag archive/\"$1\"; git push origin archive/\"$1\"; echo Deleting the local branch $1; git branch -d $1; echo Deleting the remote branch $1; git push origin --delete $1; echo Done. To restore the closed branch later, enter: git checkout -b MyNewBranch archive/\"$1\"; }; w"
closebranchpassive = "!w() { echo Attempting to close local and remote branch: $1 Processing...; echo Checking the branch $1 out..; git checkout $1; echo Trying to create a new tag archive/$1; git tag archive/\"$1\"; git push origin archive/$1; echo Deleting the local branch $1; echo Deleting the remote branch $1; echo Done. To restore the closed branch later, enter: git checkout -b MyNewBranch archive/\"$1\"; }; w"
closeremotebranch = "!w() { echo Attempting to close remote branch: $1 Processing...; echo Checking the branch $1 out..; git checkout $1; echo Trying to create a new tag archive/$1; git tag archive/\"$1\"; git tag archive/\"$1\"; echo Deleting the remote branch $1; git push origin --delete $1; echo Done. To restore the closed branch later, enter: git checkout -b MyNewBranch archive/\"$1\"; }; w"
这使用 Git shell 函数来使用更简单的语法来删除 Git 分支。
您可以决定仅删除远程分支,或者同时删除远程和本地分支,以“被动”或更强制的方式,如图所示。
旧分支远程保存为带有前缀名称'archive/'的标记分支,因此例如Azure Devops或类似的Git客户端/(基于Web的)repo浏览器将在列出分支时在文件夹结构中显示标记。
要使用它,您可以像这样关闭分支“feature/#123-somefeature-branch”:
git closebranch feature/#123-somefeature-branch
这将:
- 查看本地仓库的分支 - 警告 - 考虑在此处添加一个 git pull 以确保您的本地分支副本包含所有更新
- 标记已签出的分支
- 将标签推送为名为'archive/[branchname]'的标签
- 删除远程分支
- 删除本地分支
你最终会处于一个不正常的状态,你会看到以后如何恢复你的“关闭”分支,它现在作为一个远程标签存在,可以检出并恢复到一个分支,即
git checkout -b feature/#123-somefeature-branch-restoredalive-again archive/feature/#123-somefeature-branch