【问题标题】:How to close a branch WITHOUT removing it from history in git?如何关闭分支而不将其从 git 的历史记录中删除?
【发布时间】:2012-04-20 08:42:21
【问题描述】:

我想提交并关闭其分支,不将其从历史记录中删除

使用 mercurial,我会 commit --close-branch,然后 update 到前一个,然后继续工作。用 git...我很困惑。

【问题讨论】:

  • 不用说,搜索这些关键字我大多发现相反,即人们想从历史中删除东西。
  • 我认为造成这种混乱的是术语。在 git 中,我认为您永远不会关闭分支。 mercurial 中的“close-branch”到底是做什么的?
  • 在 mercurial 中关闭一个分支的一个很棒的效果是:没有人可以继续推动那个头。 – 有 git 方式吗??
  • @RobertSiemer 不是开箱即用的,但应该有扩展或东西来处理这种事情
  • +Robert Siemer:你错了。没有什么能阻止你将一个新的头推到一个封闭的分支上。如果已经有不同的头部,那么他们需要合并或强制推送,但他们可以这样做。在 Mercurial 中关闭分支的唯一操作是设置一个标志,以便 Mercurial 默认情况下可以从“hg 分支”中隐藏该分支。在 Git 中,您可以删除本地分支并将历史记录留在远程。或者标记分支头,到处删除分支。您有多种选择。

标签: git branch


【解决方案1】:

在 Git 中没有完全等同于关闭分支的方法,因为 Git 分支比 Mercurial 中的更轻量级。他们的 Mercurial 等价物是书签多于分支。

如果我理解正确,在 Mercurial 中关闭一个分支大致会使它从分支列表中消失,因此您可以通过归档它来实现相同的目的。一个usual practice是把它的tip标记为存档,然后删除:

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

分支将被删除,稍后可以通过签出标签并重新创建分支来检索:

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

【讨论】:

  • 至少在 git 1.9.1 中我不得不使用 git branch -D 而不是 -d
  • 如果对应的分支还没有完全与master合并,则需要-D。
【解决方案2】:

我创建了一个 powershell 脚本来自动化这个过程。此脚本存档 3 个月或更长时间的分支。您可以更改正则表达式(第 11 行)以匹配您的存档策略。

#Get all branches on remote and place in array. Format of strings in array is for example "3 months ago|origin/branch-name"
$branches = git branch -r --sort=-committerdate --format="%(committerdate:relative)|%(refname:short)|%(refname:lstrip=3)"
#Loop through all branches
ForEach ($branch in $branches)
{
  #split the branch between last commit time and branch name
  $split = $branch.Split("|")
  try
  { 
    #check if the last commit date is 4 months or more
    if($split[0] -match "((^([4-9]|10|11|12) month)|year)")
    {
      $splitBranch = $split[1].Split("/")
      #tag the branch
      git tag archive/$split[2] $split[1]
      #delete the branch
      git push --delete $splitBranch[0] $split[2]
      #add the archived branch name to a text file
      Add-Content .\archived.txt $split[1]
    }
  }
  catch
  {
    #log any branches that failed
    Add-Content .\archiveFailures.txt $split[1]
  }
}
#push all newly created tags
git push --tags

#to restore archived branch
#git checkout -b <branchname> archive/<branchname>

【讨论】:

    【解决方案3】:

    我的经验是 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-09
      • 2016-03-13
      • 2014-11-25
      • 2015-01-18
      • 2014-09-29
      • 2017-10-01
      • 1970-01-01
      • 2017-06-16
      相关资源
      最近更新 更多