When should i create a new branch?
You should create a new branch when you're doing development work that is somewhat experimental in nature. So in your scenario definitely create a new branch and not a folder within master. If you created your sandbox work as a directory in the master, it's going to reside there until you remove it using git. Having dead code sitting in the master branch is not ideal by any means, since it can confuse other developers and may just sit there rotting for the lifetime of the application.
If your team were to experience a bug, you wouldn't want them to waste time exploring the experimental work to determine if the bug existed within that directory. Creating a new branch allows you to isolate your changes from the master branch. If your experimentation goes well you always have the option to merge your changes into the master branch. If things don't go so well you can always discard the branch or keep it within your local repository.
Branches have many uses, and it depends a bit on your workflow. Two commonly used workflows are:
- github flow
Both use so called topic branches to build new features in, which get merged back once ready / accepted.
Github flow is fairly simple, and is obviously what github uses. Gitflow is a bit more complex, and is more suited when you need to support several versions of an app, where hotfixes can be applied.
In the end, it's a matter of preference what kind of workflow you use, but because creating branches is very cheap in git, it doesn't really matter how many branches you create (and eventually, delete again).
You should consider creating new branches and work on them in these cases:-
When you want to work/test out something in a sandbox environment.
It is good practice to keep your commits short in nature So frequent commits from you might disturb others' development area So its better to complete your work on a branch and then later merge your branch into main branch. [Tip] Remember to keep your branch in sync with main branch by merging main branch into your branch frequently. So at later point of time, you don't have a lot of stuff to merge by hand.
You want to solve a bug. Its better to solve it on other branch and merge it later.
If your commit goes wrong/broke your build, the production build is not affected. So I prefer to use atleast two branches dev branch and prod branch. When everything is fully tested, merge your dev branch to production branch.