Git branches & pull requests

Learning Objectives

Lesson

Up until now you have worked on a single (default) git branch, 'main'. This was fine when you were working independently, but now we are working as a team we need to isolate our development work until we consider it 'ready for review', so as not to affect other team members using the repository.

Each repository has one default branch and can have multiple other branches. You can merge a branch into another branch using a Pull Request.

Best practice is to create a new branch for each issue you work on. You should include the issue number in the name of the branch as well as a short descriptive name, for example '3-data-model'.

New branches can be created either via the GitHub UI (recommended) or via the command line using the git checkout -b command.

To work on a specific branch you need to 'checkout' that branch:

git checkout nameOfBranch

To confirm which branch you are working on, run:

git branch

This will show a * against the branch you have checked out.

You can now safely commit & push to your branch without affecting anyone else in your repository. Do ensure you push regularly just in case your local machine crashes!

If you execute git push without any arguments, it will push the changes to the currently checked out branch (which is typically what you want).

When you have finished working on your branch, make a 'Pull Request' and assign it to a reviewer (someone else from your team must look through your code before it is merged). Move the associated 'Issue' card into the 'In review' column. You can now start work on another Issue.

If you are asked to be a reviewer you should review the code carefully, check that it is well documented and has unit tests. Ask if you have any questions. When you are happy you can 'merge' the Pull Request and select to 'delete' the branch - merging will apply the code changes to the 'main' branch. No-one should ever merge their own code into main.

Assignment

  1. Use the GitHub UI to create a branch for each of the team members
  2. Clone the repository if you haven't already
  3. On the command line (within the directory where you cloned the repository), run git checkout nameOfYourBranch
  4. Run git branch and ensure you see a * next to your branch name
  5. Add a new file (can contain anything), commit it and push it - try to view the changes in the GitHub UI
  6. Create a Pull Request and assign it to another team member to review (make sure everyone gets a turn at being a reviewer)
  7. As a reviewer, use the GitHub UI to see what changes have been done, when you are happy, 'Merge' the Pull Request and delete the branch. If you are not happy, add comments stating why to the Pull Request and assign the Issue back to 'In Progress'.
  8. When your code is merged, switch back to the 'main' branch using git checkout main and run git pull to get the latest changes. Delete your old branch using git branch -D nameOfYourMergedBranch.