💻Lab: Git branches

Git branches and pull requests

This activity is to be done in pairs or groups of three, please share your screen for the parts which only one person can do.

Step 1: Create git repo

One person should create the git repository, use https://github.com/new

Please choose the following settings:

  • Make it private

  • Initialise it with a README

  • Choose the Node template for your .gitignore

Once it's created, go to the Settings for that repo, and then Branches.

Add a branch protection rule for your main branch. (Confusingly the GA Git will use 'master' by default and the public github will use 'main' but they are the same thing.)

The rule you want is Require pull request reviews before merging. This means no one will be able to commit changes to the master/main branch, except by creating a pull request.

Step 2: Everyone clone the repo

In the settings, you'll need to add your group-mates as collaborators to the repo too.

Get everyone to do the following:

  • Clone the repo to their own computer: git clone <url of the repo> Make sure you use the SSH URL

  • Create a new branch with a different name git branch <branchname>

  • Switch to your new branch with git checkout <branchname>

  • Everyone make a change to add a file, or edit the README, commit it and push it

    • It's the first time pushing this branch, so follow the instructions it gives you to create the branch on github too.

When everyone has done this, look at the branches on GitHub and see that there are different. Notice that the main branch hasn't changed.

Step 3: Everyone creates a pull request

When looking at your branch on GitHub, there should be options to "compare" or "create pull request".

For your own change:

  • Create a pull request (PR)

  • Assign the others in your group to be the "Reviewers" for your PR

  • On your own computer, make the changes that your teammates ask for in the comments

  • When you've made the changes, commit and push it again.

  • When your PR is approved, click that "merge" button.

As a Reviewer:

  • Have a look at the change in the PR and add a comment (ask them to change something)

  • When you can see that your teammate has made the change that you asked for "Approve" the PR

Step 4: Pull the changes, clean up, start again!

You'll need to:

  • Delete your branch on github (click the delete branch button once you've merged the PR)

  • Switch back to the main branch on your computer with git checkout main

  • Run git pull (check that you get all the changes, including your own change!)

  • Delete your old branch on your computer too (git branch -D <branchname>)

Now you're ready to start working on your next change!

Extension:

Have two people make different changes to the same part of the same file in their branches.

Go through the same process as above with both changes and see what happens!

Take your time and be careful to read what git tells you to do. Grab an instructor if you need help sorting it out.

Last updated