how to close a branch in git
Asked Answered
C

6

65

When I know I won't use a branch any more is it possible to close or lock it? Actually I would like to close a branch but I don't think it is possible to close a branch with GIT. what about the delete. What happens to my history when I delete a branch?

Castor answered 2/3, 2016 at 4:53 Comment(1)
Does this answer your question? How can I archive git branches?Cady
M
82

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>
Melodie answered 2/3, 2016 at 5:2 Comment(1)
You might also want to push your tag to the server to share it with everyone. To do so just use git push archive/<branchname> or to push all your tags git push --tagsCommemorate
B
9

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

Bujumbura answered 2/3, 2016 at 7:49 Comment(0)
B
1

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.

Beaumont answered 10/6, 2021 at 15:38 Comment(0)
H
0

If you want to delete a branch completely, you can just delete it in all your repositories (typically local and remote). git will then clean it up next time it garbage collects.

See How do I delete a Git branch both locally and remotely? for instructions.

Hach answered 2/3, 2016 at 8:35 Comment(0)
S
0

You can put this under alias in your active .gitconfig file, alter the formatting as I inserted newline after the semi colons for readability here. The trick I am using here is defining a shell function that Git can use and issue multiple commands against Git separated by semi colons.

   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"

The alias is a set of commands that will when you run: git closebranch SomeBranch

  1. Check out the branch $1 called SomeBranch
  2. Create a new tag called archive/$1 as a tag, here called "archived tag"
  3. Push to origin the created tag (note that I here assume your remote is called origin, type git remote -v to check out if that is so.
  4. Delete the branch $1
  5. Delete the remote branch $1
  6. Explain to the developer how to restore the branch later from the archived tag later into a new branch.

This will hopefully help developers in avoiding branches to heap up and avoid clutter as Git lacks a standard way of closing branches such as Mercurial got.

Samuelsamuela answered 14/6, 2020 at 21:22 Comment(0)
B
0

In addition to the comments above, git flow should also be mentioned. If you have opened a branch for an update, you can simply remove it with the following command and merge it in the develop branch:

Branch: "feature-bootstrap5"

$ git flow feature finish feature-bootstrap5

More Infos: https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow https://danielkummer.github.io/git-flow-cheatsheet/index.html

Bedlamite answered 3/5, 2021 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.