how to close a branch in git

Updated Answer

As @user3159253 stated in comments of this answer :

git garbage-collects commits which aren't referenced, directly or indirectly, by a named reference (branch, tag, etc). That is why it is important to leave a reference to a freezed branch.


You can tag the tip of the branch by archiving it, and then delete the branch.

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

Or more simply :

git checkout -b new_branch_name archive/<branchname>

Even after merging a branch, there are still circumstances that keeping the information for All the commits may be critical...

Consider you are working on a feature branch, and decide to add some code to make complicated calculations as part of the development. After the analysis this code is no longer needed for moving forward so the code is removed. Now the branch is merged (the analytics are not part of it).

2 years later, you need to get that analytics code to prove you did due diligence during the development of the feature.... but it is gone, you are su ed/fined, not bankrupt your business closes.


you can refer to git finding unmerged branches to find those branch that have been merged or not.

If a branch has been merged into other branch, it is safe to delete it use the follwing command.

git branch -D branch_name