Chuck's Academy

Git and GitHub

Pull Requests and Code Reviews

In this module, we will learn how to use Pull Requests (PRs) and perform code reviews on GitHub. PRs are fundamental for collaborative work as they allow us to review and discuss changes before merging them into the project's main branch.

What is a Pull Request?

A Pull Request (PR) is a request to merge changes from one branch to another. It allows other team members to review, discuss, and approve the changes before merging. PRs are essential for maintaining code quality and ensuring that all changes are reviewed before being integrated into the main branch.

Creating a Pull Request

Step 1: Make changes and push them to a branch

First, make the changes in a new branch and push those changes to the remote repository.

bash

Step 2: Create the PR on GitHub

  1. Navigate to the repository page on GitHub.
  2. Click on the "Pull requests" tab.
  3. Click on "New pull request."
  4. Select the branch with your changes (e.g., new-feature) and the target branch (e.g., main).
  5. Provide a title and a detailed description of the PR.
  6. Click on "Create pull request."

Code Review

Assigning Reviewers

Once the PR is created, you can assign reviewers to inspect your code:

  1. On the PR page, look for the "Reviewers" section.
  2. Assign one or more reviewers who will review your PR.

Review Process

Reviewers can make comments on specific lines of code, suggest changes, and approve or request modifications.

Line Comments

Reviewers can leave comments directly on lines of code:

  1. In the PR view, click on the specific lines of code.
  2. Leave a comment explaining the suggestion or issue.

Requesting Changes

If the PR needs adjustments, reviewers can request changes:

  1. At the top of the PR view, click on "Files changed."
  2. Click the "Review changes" button.
  3. Select "Request changes," add a comment, and click "Submit review."

Approving Changes

If everything is correct, reviewers can approve the PR:

  1. At the top of the PR view, click on "Files changed."
  2. Click the "Review changes" button.
  3. Select "Approve" and click "Submit review."

Merging a Pull Request

Merge the PR

Once the reviewers approve the PR, you can merge it:

  1. On the PR page, click the "Merge pull request" button.
  2. Confirm the merge by clicking "Confirm merge."

Delete the Merged Branch

After merging the PR, it is good practice to delete the branch that is no longer needed:

  1. On the PR page, click "Delete branch."

Best Practices for Pull Requests and Code Reviews

  • Clear titles and descriptions: Provide detailed titles and descriptions so reviewers understand the purpose of the changes.
  • Small and consistent commits: Make small and descriptive commits to facilitate the review.
  • Respond to comments: Always respond to reviewers' comments, either accepting suggestions or explaining your reasoning.
  • Resolve conflicts before merging: If the PR has conflicts, resolve them before requesting a final review.

Additional Tools

Using PR Templates

You can create PR templates to ensure all necessary details are included:

  1. Create a .github/pull_request_template.md file in your repository.
  2. Add a description with sections that you want to be completed in each PR.

Automation with GitHub Actions

Set up GitHub Actions to automate tests, code analysis, and other tasks when a PR is created or updated.

yaml

Practical Example

  1. Create and work on a new branch:

    bash
  2. Create a Pull Request on GitHub.

  3. Assign reviewers and receive comments.

  4. Make additional changes if necessary and push:

    bash
  5. Review and approve the PR.

  6. Merge the PR and delete the branch.

With these techniques and workflows, you can effectively manage PRs and code reviews on GitHub, ensuring that all changes are reviewed and approved before being merged into the main branch. In the next module, we will learn about advanced Git usage, including rebase and cherry-pick.


Ask me anything