46

I am starting a new project, and to experiment a little bit with the technologies used in the project (hello world examples...) I want to create something like a "playground" branch. Is it common to create a new branch "playground" or should I just create a folder named "playground" in the master branch?

5 Answers 5

48

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.

2
  • 10
    "You should create a new branch ... " always, period. Mar 26, 2013 at 10:03
  • Imagine my project is totally broken and now, I decide to rewrite the project entirely but all developments and improvements have to be in the same repo. Is It common to create a new branch (named like "re-creation") and then merge it to the main branch? If it's not, do you have any other suggestions about that scenario?
    – Sadra
    Nov 3, 2021 at 1:39
12

Branches have many uses, and it depends a bit on your workflow. Two commonly used workflows are:

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).

10

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.

5

You should defiantly learn by experimenting but in a playground repo.
A playground directory in a repo will be will less benefit.
Have a play, make some mistakes learn some things - delete it a few times and go wild.

e.g.

$ mkdir playground  
$ git init
$ touch hello-world
$ git add hello-world
$ git commit -m "my first commit"
$ git branch goodbye
$ git checkout goodbye
$ echo "goodbye" | cat >>hello-world
$ git status
$ git add hello-world 
$ git commit -m "goodbye commit"
$ git merge master

I'd also reccomend grabbing a fork of an active project in the language of your choice from GitHub and give a go with merging, rebasing etc there with real code.

2

The common way is to use the master branch as live branch. Then you make new branches from the master and work on them (feature branches). After you have finished your work you merge the changes back to your master.

You can build new remote branches like a dev branch and merge your changes to this branch when you want only a preview. There are some methods but there are a lot of informations on google.

Branching and merging
Git branching model

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.