6: Creating a Branch#
In this chapter, you’ll learn how to create a new branch, make and commit changes in that branch, and then switch back to the main branch using GitHub Desktop.
Git allows you to work on multiple lines of development simultaneously by creating branches. This is useful when you want to work on a new feature, fix a bug, or experiment with changes without affecting the main codebase. Once you are satisfied with the changes in the branch, you can merge it back into the main branch.
Another useful feature of Git is that you can go back and forth between branches. This is called “branch switching” or “checking out” a branch. This allows you to work on different features or fixes independently and switch between them as needed.
Table of Contents#
Creating a Branch#
In GitHub Desktop, click on the
Current Branchdropdown in the top toolbar.Select
New Branch...from the dropdown menu.

In the
Namefield, enter a name for your new branch (e.g.,awesome-featureorfeature/awesome-feature). some common branch naming conventions include:feature/branch-name: for new featuresbugfix/branch-name: for bug fixeshotfix/branch-name: for critical fixesrelease/branch-name: for release preparationdocs/branch-name: for documentation changes
Click the
Create Branchbutton.

Making and Committing Changes in the New Branch#
With the new branch selected, click the
Repositorymenu and selectOpen in <Your Default Editor>to open the repository in your text editor.Make changes to your files or create new files as needed. For example, you could create a new file called
awesome-feature.mdand add some content to it.
Example Content for awesome-feature.md:#
# Awesome Feature
This is the new feature we are adding to our project. It will enhance the user experience and improve performance.
Save your changes in the text editor.
Return to GitHub Desktop. You should see your changes listed under the
Changestab.In the
Summaryfield, write a brief description of the changes you made (e.g., “Add new feature file awesome-feature.md”).Optionally, add a more detailed description in the
Descriptionfield.Click the
Commit to <your-branch-name>button to commit your changes.

Checkout the Main Branch#
With Git you can switch back to the main branch easily (or another branch). You can do that anytime your code is committed and stable. You can also do this if you have uncommitted code, but that code will be lost if you switch branches without committing it, or use a more advanced Git feature like stashing.
Click on the
Current Branchdropdown in the top toolbar.Select
main(ormaster, depending on your default branch name) from the dropdown menu to switch back to the main branch.

Verify that you are now on the main branch and your previous changes are not present in this branch, and if you look in your text editor, you should see the changes you made in the new branch are no longer visible (such as the
awesome-feature.mdfile not being present).
