Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to close a branch in git

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?

like image 872
Matthieu Avatar asked Mar 02 '16 04:03

Matthieu


People also ask

How do I close a branch in git?

Delete a branch with git branch -d <branch> . The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet.

How do I close a remote branch?

To delete a remote branch, you can't use the git branch command. Instead, use the git push command with --delete flag, followed by the name of the branch you want to delete. You also need to specify the remote name ( origin in this case) after git push .

Should you close git branches?

They're unnecessary. In most cases, branches, especially branches that were related to a pull request that has since been accepted, serve no purpose. They're clutter. They don't add any significant technical overhead, but they make it more difficult for humans to work with lists of branches in the repository.


1 Answers

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> 
like image 54
Farhad Faghihi Avatar answered Oct 06 '22 00:10

Farhad Faghihi