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?
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>
git push archive/<branchname>
or to push all your tags git push --tags
–
Commemorate 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
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.
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.
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
- Check out the branch $1 called SomeBranch
- Create a new tag called archive/$1 as a tag, here called "archived tag"
- 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.
- Delete the branch $1
- Delete the remote branch $1
- 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.
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
© 2022 - 2024 — McMap. All rights reserved.