Branching

Branching

Branching facilitates working on different parts and versions of a repository at the same time. By default, each repository is created and initiated with a main branch. You can commit directly into the main branch as we just did.

A branch is an identical copy of the main repository (or any other branch you want to copy from). Branching allows you to implement and test changes before committing them into the main repository (your production branch).

Branches are copies of a repository. They allow to develop the repository in different directions.

Creating a new branch

Let’s play with our newly created repository.

Challenge: create a branch from your main repository

1. Go to your new repository and click the drop down at the top of the file list that says branch: main

The default branch used to be called master branch; due to historic connotations of the word master and as part of a step towards more inclusive language the default branch has been renamed to main for new repositories. Older repositories still might have a master branch.

  1. Create a branch Readme-edits. This branch will be a copy of your main branch.

  2. Switch to your new branch.

  3. Click the edit button of the Readme.md file. Add a line to the Readme.md file.

  4. Commit your changes. In this case I have removed a line.

  5. You should now see that your branch has recent edits.

  6. Switch back to the main branch. You should see that the Readme.md file is in the state it was before branching.

  7. Switch back to the Readme-edits branch.

Branches are a good way to test edits and changes to your repository and see whether you like them. If you are not happy with your changes, you can just disregard your branch and the changes will never affect the content of your main repository.

Different branches can develop independently and in parallel of each other.