How to close a branch WITHOUT removing it from history in git?
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.
#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>
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.
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 practice is to tag its tip as archive, and delete it:
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