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
- Navigate to the repository page on GitHub.
- Click on the "Pull requests" tab.
- Click on "New pull request."
- Select the branch with your changes (e.g.,
new-feature
) and the target branch (e.g.,main
). - Provide a title and a detailed description of the PR.
- Click on "Create pull request."
Code Review
Assigning Reviewers
Once the PR is created, you can assign reviewers to inspect your code:
- On the PR page, look for the "Reviewers" section.
- 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:
- In the PR view, click on the specific lines of code.
- Leave a comment explaining the suggestion or issue.
Requesting Changes
If the PR needs adjustments, reviewers can request changes:
- At the top of the PR view, click on "Files changed."
- Click the "Review changes" button.
- Select "Request changes," add a comment, and click "Submit review."
Approving Changes
If everything is correct, reviewers can approve the PR:
- At the top of the PR view, click on "Files changed."
- Click the "Review changes" button.
- Select "Approve" and click "Submit review."
Merging a Pull Request
Merge the PR
Once the reviewers approve the PR, you can merge it:
- On the PR page, click the "Merge pull request" button.
- 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:
- 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:
- Create a
.github/pull_request_template.md
file in your repository. - 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
-
Create and work on a new branch:
bash -
Create a Pull Request on GitHub.
-
Assign reviewers and receive comments.
-
Make additional changes if necessary and push:
bash -
Review and approve the PR.
-
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
.
- Introduction to Git and GitHub
- Installation and Configuration of Git
- Version Control Fundamentals
- Repository Creation and Cloning
- Making Commits and Tracking Changes
- Branch Management (branching)
- Branch Merging (Merging)
- Conflict Resolution
- Collaborative Work on GitHub
- Pull Requests and Code Reviews
- Advanced Git Usage (rebase, cherry-pick, etc.)
- Automation with Git hooks
- Continuous Integration with GitHub Actions
- Version Management and Release Deployment
- Conclusions and Best Practices in Git and GitHub