A branch lets you develop code, fix a problem, or test an idea without affecting the original project. A branch is created from an existing branch, usually from the main branch of the project. Note that main branch was previously named master branch, but removed because of the reference to slavery. Because of this change you might find references to the old name when looking for help elsewhere.
You can work on a branch, bravely develop code that works or does not work and it does not break the code on the main branch. Basically, you can experiment on your own.
If the idea did not work or you have another idea, the branch can be deleted and nobody ever needs to know about it. Make a new branch and start all over again. None of this will affect the original project.
If you are working on your own project, and happy with the new code, or the idea is tested and working, you can merge the branch you are working on with the main branch.
To understand the workflow, let’s see how Kingsley is using branches (see Figure 1.1).
Kingsley owns a GitHub repo. He clones the repo to his computer (1). To develop code, he create a new branch (2) and makes commits (3). Then he merges the new branch with the main branch locally on his computer (4). Finally, he pushes these changes to his remote repo on GitHub (5). Kingsley then continue the workflow (2-5).
Figure 1.1: Workflow using branches
Let’s have a look how to create, use and delete branches.
In RStudio, click on the New Branch button in the Git tab. Next to this button it will say on which branch you are. In this case it is main, which is the default branch.