如何在git中关闭分支而不将其从历史记录中删除?

时间:2012-04-20 08:42:21

标签: git branch

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

对于mercurial,我commit --close-branch,然后update到前一个,继续工作。随着git ...我很困惑。

2 个答案:

答案 0 :(得分:73)

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

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

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

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

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

答案 1 :(得分:1)

我创建了一个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>