Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is creating and removing lots of branches in git bad?

I have a main branch master that contains the stable code. Each time I want to build something new I get checkout -b 'development', add the new code, then merge it back into the master and delete the development branch.

However, is this creating a larger repo database than just keeping the development branch and merging master into it (so development is up-to-date) before I go to add something new? Do all these temp development branches take extra space in the repo?

like image 802
Xeoncross Avatar asked Nov 07 '10 19:11

Xeoncross


People also ask

Is it good practice to delete Git branches?

It's a common housekeeping practice to delete git branches once they're no longer used, but this practice isn't necessarily universal, or universally understood.

Why long running branches are bad?

This has two effects: you cannot test how code in the long lived branch plays with other code under development. painful merge conflicts become a recurring event when the long lived branch gets merged into the mainline.

Is it good practice to delete branch after merge?

When you're done with a branch and it has been merged into master, delete it. A new branch can be made off of the most recent commit on the master branch. Also, while it is ok to hang onto branches after you've merged them into the master they will begin to pile up.

Can you delete multiple branches in git?

Remove all Git branches but main Newer Git repositories have renamed the master branch to main. To delete all branches in Git except main, simply replace the grep for master with a grep for main: git branch | grep -v "main" | xargs git branch -D. git branch | grep -v " main$" | xargs git branch -D.


2 Answers

Creating temporary branches for single topics/features/tasks is good. You're doing it right. Don't just stop with "development" - "featureA", "bugX", "bugX-testing", anything you need, make it!

A branch takes up essentially no space. It's a pointer to the commit at its tip, so it's represented as a file (.git/refs/heads/branch-name) whose contents are simply the SHA1 of that commit. Teeny tiny. Eventually they can get gathered up into the packed-refs file, which is just a line per branch - name and SHA1, even smaller than the original! (Git does this to avoid having too many files in a repo which has, say, 1000 minor/maintenance release version tags in its history.) They do also have reflogs, which record a line each time the position of the branch changes (try git reflog show to see the reflog for HEAD), but again, that's pretty small - and it's removed when you remove the branch.

like image 115
Cascabel Avatar answered Nov 02 '22 03:11

Cascabel


As Jefromi explained, branches are designed to be ridiculously cheap. Not only that, as far as I know, Git is the only major, free VCS that does branching this way.

Given how you seem to be using branches, I think you'd benefit from nvie's GitFlow branching model and helper extension (link to explanatory blog post) as a simple, clean way to make the most of branching for release management. (Basically, a more polished approach to what you seem to already be doing)

like image 23
ssokolow Avatar answered Nov 02 '22 03:11

ssokolow