ChatGPT解决这个技术问题 Extra ChatGPT

What to do with branch after merge

I had two branches: master and branch1. I just merged branch1 back into master and I'm done with that branch. Should I delete it or just let it sit around? Will deleting it cause any loss of data?


J
Jonas Schäfer

After the merge, it's safe to delete the branch:

git branch -d branch1

Additionally, git will warn you (and refuse to delete the branch) if it thinks you didn't fully merge it yet. If you forcefully delete a branch (with git branch -D) which is not completely merged yet, you have to do some tricks to get the unmerged commits back though (see below).

There are some reasons to keep a branch around though. For example, if it's a feature branch, you may want to be able to do bugfixes on that feature still inside that branch.

If you also want to delete the branch on a remote host, you can do:

git push origin :branch1

This will forcefully delete the branch on the remote (this will not affect already checked-out repositiories though and won't prevent anyone with push access to re-push/create it).

git reflog shows the recently checked out revisions. Any branch you've had checked out in the recent repository history will also show up there. Aside from that, git fsck will be the tool of choice at any case of commit-loss in git.


If you want to save the branch you can create a tag before deleting it. The if you want to go back to that point you can checkout that tag.
@Joqus I’d rather suggest to keep it as a branch in that case. Just don’t publish it.
Just in case - if you want to "completely merge" the branch before deleting it, just push that branch after merging, but before deleting. Something like git checkout master && git merge branch1 && git push origin branch1 && git branch -d branch1
What about the history of the branch you deleted? How is it safe to delete the branch with this in mind?
a
ahmednabil88

I prefer RENAME rather than DELETE

All my branches are named in the form of

Fix/fix- or

Ftr/ftr- or

etc.

Using Tower as my git front end, it neatly organizes all the Ftr/, Fix/, Test/ etc. into folders.
Once I am done with a branch, I rename them to Done/...-<description>.

That way they are still there (which can be handy to provide history) and I can always go back knowing what it was (feature, fix, test, etc.)


Do you also rename the remote branches?
It seems redundant to keep these branches, and insane to rename them. Am I missing something here? You can always recreate the branch. stackoverflow.com/questions/3640764/…
makes no sense to keep branches after they were merged
+1 for the Idea on renaming to Fix, Test and Done... Keeping branches is how contractors get paid on my company.
@ksav if you do, will it have the history that it previously had before merge then deletion?
m
mhck

If you DELETE the branch after merging it, just be aware that all hyperlinks, URLs, and references of your DELETED branch will be BROKEN.