Managing GitHub Pages Branches

Tutorial 3 of 5

Introduction

Welcome to this tutorial on how to manage branches for your GitHub Pages site. By the end of this tutorial, you will have a solid understanding of how to create, switch between, and delete branches. You will also learn how to use branches to manage different versions of your site.

In order to follow along, you will need to have a basic understanding of Git, as well as a GitHub account. Some familiarity with GitHub Pages will also be helpful.

Step-by-Step Guide

What is a Branch?

In Git, a branch is essentially a unique set of code changes with a unique name. Each repository can have one or more branches. This allows you to move back and forth between 'states' of a project. For example, if you want to add a new page to your website, you can create a new branch just for that page without affecting the main part of the project (which is typically the master or main branch).

Creating a Branch

To create a branch, you can use the git branch command followed by the name of the new branch. For example:

git branch new-page

This will create a new branch called new-page.

Switching Between Branches

To switch between branches, you can use the git checkout command followed by the name of the branch. For example:

git checkout new-page

This will switch to the new-page branch.

Deleting a Branch

To delete a branch, you can use the git branch -d command followed by the name of the branch. For example:

git branch -d new-page

This will delete the new-page branch.

Code Examples

Let's go through some practical examples.

Creating and Switching to a New Branch

git branch new-page   # Creates a new branch called "new-page"
git checkout new-page # Switches to the "new-page" branch

Making Changes and Committing Them

echo "Hello, world!" > index.html      # Creates a new file called "index.html" with the content "Hello, world!"
git add index.html                     # Stages the new file for commit
git commit -m "Add index.html"         # Commits the changes with a message

Pushing Changes to GitHub

git push origin new-page               # Pushes the changes to the "new-page" branch on GitHub

Deleting a Branch

git checkout master                    # Switches back to the "master" branch
git branch -d new-page                 # Deletes the "new-page" branch

Summary

In this tutorial, we've learned how to manage branches in GitHub Pages. We've covered how to create, switch between, and delete branches. We've also learned how to make changes to a branch, commit those changes, and push them to GitHub.

For further learning, consider exploring more advanced Git topics such as merging branches, resolving conflicts, and using Git tags.

Practice Exercises

  1. Exercise 1: Create a new branch called about-page, switch to it, create a new file called about.html with some content, stage and commit the changes, then push them to GitHub.

  2. Exercise 2: Create a new branch called contact-page, switch to it, create a new file called contact.html with some content, stage and commit the changes, then push them to GitHub. Then, switch back to the master branch and delete the contact-page branch.

Solutions:

  1. Solution 1:

    bash git branch about-page git checkout about-page echo "This is the about page." > about.html git add about.html git commit -m "Add about.html" git push origin about-page

  2. Solution 2:

    bash git branch contact-page git checkout contact-page echo "This is the contact page." > contact.html git add contact.html git commit -m "Add contact.html" git push origin contact-page git checkout master git branch -d contact-page

To further practice, try creating more branches, making more complex changes, and experimenting with different Git commands.