git 如何关闭分支而不将其从git的历史记录中删除?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10242924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to close a branch WITHOUT removing it from history in git?
提问by o0'.
I'd like to make a commit and close its branch, without removing it from history.
我想提交并关闭它的分支,而不是从 history 中删除它。
With mercurial I'd commit --close-branch
, then update
to a previous one, and go on working. With git... I'm confused.
使用 mercurial Id commit --close-branch
,然后update
转到前一个,然后继续工作。使用 git ......我很困惑。
回答by CharlesB
There's no exact equivalent to closing a branch in Git, because Git branches are more lightweight than in Mercurial. Their Mercurial equivalent is more bookmarks than branches.
没有完全等同于关闭 Git 中的分支,因为 Git 分支比 Mercurial 中的更轻量级。它们的 Mercurial 等价物是书签而不是分支。
If I understand correctly, closing a branch in Mercurial roughly makes it disappear from the branch list, so you can achieve the same thing by archiving it. A usual practiceis to tag its tip as archive, and delete it:
如果我理解正确的话,在 Mercurial 中关闭一个分支大致会使它从分支列表中消失,所以你可以通过归档它来实现同样的目的。一个通常的做法是将其标记为归档尖,并将其删除:
git tag archive/<branchname> <branchname>
git branch -d <branchname>
git checkout master
The branch will be deleted, and can be retrieved later by checking out the tag, and recreating the branch:
分支将被删除,稍后可以通过检出标签并重新创建分支来检索:
git checkout archive/<branchname>
git checkout -b new_branch_name
回答by garethb
I've created a powershell script to automate the process. This script archives branches 3months old or more. You can change the regex (line 11) to match your archive strategy.
我创建了一个 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>