64

I'd like to make a commit and close its branch, without removing it from history.

With mercurial I'd commit --close-branch, then update to a previous one, and go on working. With git... I'm confused.

5
  • 1
    Needless to say, searching for these keywords I mostly found the other way around, i.e. people wanting to remove stuff from history.
    – o0'.
    Apr 20, 2012 at 8:43
  • 1
    I think what makes this confusing is the terminology. In git I don't think you ever close a branch. What exactly does "close-branch" do in mercurial? Apr 20, 2012 at 9:16
  • 4
    One awesome effect of closing a branch in mercurial is: no-one can continue pushing on that head. – Is there a git-way for that?? Jun 15, 2015 at 10:32
  • @RobertSiemer not out of the box, but there should be extensions or stuff to handle that kind of thing
    – o0'.
    Jun 15, 2015 at 10:38
  • 1
    +Robert Siemer: You're wrong about that. Nothing prevents you from pushing a new head to a closed branch. If there's already a different head then they'd need to merge or force push, but they can do that. The only thing closing a branch does in Mercurial is set a flag so that Mercurial can hide that branch by default from 'hg branches'. In Git, you can just delete your local branch and leave the history in the remote. Or tag the branch head and delete the branch everywhere. You have several options.
    – bambams
    Jun 6, 2016 at 13:21

3 Answers 3

86

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
2
  • At least in git 1.9.1 I had to use git branch -D rather than -d. Jul 25, 2016 at 13:09
  • 1
    The -D is required if the corresponding branch was not fully merged with master yet.
    – Simeon
    Apr 18, 2017 at 10:00
3

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>
2

My experience is that Mercurial has good support for closing branches, to give an example.

Git perhaps expects you to either delete branches to keep it maintainable in a way of reducing the branch count, or stick to tags. As long as you got a tag, you can restore it into a branch again. I use aliased commands with shell functions so I with ease can close a branch (archive it as a tag for later restoration, if I find use of it again).

I would suggest you only close branches you are sure will be useful later. Chances are high that you need to do a lot of merge fixing when restoring the branch and try to merge it in. But it can also be that you want to just see the state of the code in an old branch, where you 'delete it' but keep it around as a tag.

I use this approach inside my .gitconfig under [alias]

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"

This uses Git shell functions to use an easier syntax to delete a Git branch.
You can decide to either delete the remote branch only, or both the remote and local branch, either in a 'passive' or a more forced way as shown.

The old branch remotely is kept as a tag branch with the prefixed name 'archive/' so for example Azure Devops or similar Git clients / (web based) repo browsers will show the tag inside a folder structure when listing the branches.

To use it you can for example close the branch 'feature/#123-somefeature-branch' like this :

git closebranch feature/#123-somefeature-branch 

This will :

  • Check out the branch to local repo - warning - consider adding also a git pull here to be sure your local copy of the branch contains ALL updates
  • Tag the branch checked out
  • Push the tag as a tag named 'archive/[branchname]'
  • Delete the remote branch
  • Delete the local branch

Youll end up in a detacted state where you are greeted with how you can restore your 'closed' branch later on, which now exists as a remote tag, which can be checked out and restored to a branch i.e. git checkout -b feature/#123-somefeature-branch-restoredalive-again archive/feature/#123-somefeature-branch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.