Collaborating and Pull Requests
Workplace Context
Your team is working on several critical features that need to be integrated into the main project. To ensure code quality and smooth collaboration, the team relies on pull requests for reviewing and approving changes before they are merged into the main branch.
As a developer on the team, you are required to submit pull requests for the features you develop and review your teammates’ pull requests for code quality, adherence to best practices, and functionality. This process ensures that only tested and approved code makes it into the final product, reducing the risk of errors and improving the overall quality of the project.
This training will teach you how to collaborate effectively with your team using pull requests and code reviews.
Learning Objectives
By the end of this lesson, you will be able to:
- Explain the purpose and importance of pull requests in collaborative development.
- Create and submit pull requests on GitHub.
- Review and provide feedback on pull requests.
- Resolve merge conflicts during the pull request process.
What Is a Pull Request?
A pull request (PR) is a feature used in GitHub that allows developers to notify others when they have completed a feature or made significant changes to a project. It is a formal way of requesting that changes made on one branch be reviewed and merged into another branch (typically the main
or development
branch).
Pull requests are critical in team projects because they:
- Allow code reviews to ensure quality and identify potential issues.
- Facilitate collaboration by making it easy to track, comment on, and test changes.
- Help avoid errors by ensuring that changes are merged into the main project only after being reviewed and approved.
Pull requests are essentially the “quality assurance” process of development, making sure that changes have been signed off on by a reviewer before they are allowed into the main project. This can avoid catastrophic issues in industry , when done properly.
Creating a Pull Request on GitHub
Now that you have learned how to create and merge branches locally, let us move to GitHub to collaborate with your team. In this example, you will create a pull request to merge your feature branch into the main
branch.
Step-by-Step: Creating a Pull Request
-
Push Your Local Changes to GitHub:
- First, ensure your feature branch is up to date and has been pushed to GitHub:
$ git push origin feature/navigation-bar
- First, ensure your feature branch is up to date and has been pushed to GitHub:
-
Navigate to GitHub:
- Go to your GitHub repository. You should see a notification prompting you to open a pull request for your recently pushed branch.
-
Open a New Pull Request:
- Click the green New Pull Request button.
- Select the branch you want to merge into (e.g.,
main
) and the branch you want to merge from (e.g.,feature/navigation-bar
).
-
Add a Title and Description:
- Give your pull request a clear title (e.g., “Add navigation bar to homepage”).
- In the description, explain the changes you have made, why they are necessary, and any additional context that might help the reviewers.
-
Submit the Pull Request:
- Once you have filled in the details, click Create Pull Request. Your teammates will now be able to review your changes.
Reviewing and Providing Feedback on a Pull Request
When a pull request is created, it is typically assigned to one or more reviewers who will examine the code changes, run tests, and ensure that the changes meet the project’s standards.
Step-by-Step: Reviewing a Pull Request
-
Navigate to the Open Pull Requests:
- On GitHub, click the Pull Requests tab to see all open PRs for your repository.
-
Open a Pull Request:
- Select a pull request that is ready for review. You will be able to see a summary of the changes and a list of the files that have been modified.
-
Review the Code:
- GitHub provides an interface for reviewing changes. You can view the diff (differences between the changes and the current version), and add inline comments directly on specific lines of code.
-
Provide Feedback:
- As a reviewer, it is important to provide constructive feedback. If you find any issues, leave comments explaining what needs to be fixed or improved.
-
Approve or Request Changes:
- If the changes look good, you can approve the pull request.
- If there are issues, you can request changes and notify the developer.
Handling Merge Conflicts in a Pull Request
Sometimes, when multiple developers are working on the same project, you may encounter merge conflicts during the pull request process. This happens when two branches make changes to the same parts of a file, and Git cannot automatically merge them.
Step-by-Step: Resolving Merge Conflicts in a Pull Request
-
View the Conflict:
- If a conflict arises during the PR, GitHub will notify you, and the conflicting files will be highlighted. Navigate to the “Files Changed” tab to see where the conflicts are.
-
Resolve the Conflict:
-
You will see conflict markers in the file (similar to those you have resolved locally):
<<<<<<< HEAD <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Contact</a></li> </ul> </nav> ======= <footer> <p>© 2024 My Website</p> </footer> >>>>>>> feature/footer
-
Manually resolve the conflict by editing the file to keep the desired changes. For example, you can combine the navigation bar and footer:
<nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Contact</a></li> </ul> </nav> <footer> <p>© 2024 My Website</p> </footer>
-
-
Commit the Resolved File:
- After resolving the conflict, commit the changes directly through the GitHub interface by clicking Mark as Resolved, then commit.
-
Merge the Pull Request:
- Once all conflicts have been resolved and the pull request is approved, you can merge it into the
main
branch by clicking the Merge Pull Request button.
- Once all conflicts have been resolved and the pull request is approved, you can merge it into the
Activity: Practice with Pull Requests
In this activity, you will simulate a real-world collaborative development process using GitHub’s pull request (PR) system. Working on feature branches, you will:
- Submit a Pull Request: After developing a feature in a separate branch, you will create a pull request to merge your changes into the
main
branch. - Review a Pull Request: You will pair with a classmate to review each other’s code, providing feedback and ensuring the changes meet project standards before they are merged.
- Resolve Conflicts: If there are conflicts between your changes and your teammate’s changes, you will work together to resolve them before completing the merge.
This activity gives you hands-on experience with GitHub’s pull request process, reinforcing your ability to collaborate effectively in team-based development projects in the workplace.
Task: Work in a Team to Simulate the PR Process
-
Create a Feature Branch:
- Use the work you’ve done on the navigation bar or footer feature branches from previous lessons to create a pull request.
-
Submit a Pull Request:
- After completing the feature, push the branch to GitHub and submit a pull request to merge it into the
main
branch.
- After completing the feature, push the branch to GitHub and submit a pull request to merge it into the
-
Review a Teammate’s Pull Request:
- Pair up with a classmate. Each of you will create a pull request for your own feature. Review your classmate’s pull request by checking their code and leaving constructive feedback.
-
Resolve Conflicts:
- If your changes and your teammate’s changes cause a conflict, work together to resolve the conflict using GitHub’s merge conflict resolution tools.
-
Repeat the Process:
- Create another branch and develop a simple feature of your own choosing, then submit a pull request. Have your partner review the request, ensuring that it contains appropriate code and does not cause any conflicts.
Examples from Industry
React Repository
What do real pull requests look like? Open the React repository on GitHub again, and click on the “Pull requests” tab, which will bring you to the open pull requests interface .
From here, you can look at each pull request, its title, its age, any relevant tags it might have, and the number of comments on the request. Take the time to look at pull requests that:
- Have a large number of comments.
- Have been approved.
- Have been rejected.
- Have a linked issue.
Use the filters available to you to navigate through these pull requests more easily!
Knowledge Check
What is the primary purpose of a pull request in GitHub?
- Select an answer to view feedback.
What happens if there is a conflict during a pull request?
- Select an answer to view feedback.
Summary
In this lesson, you learned how to create and submit pull requests, review code changes, and resolve conflicts. Pull requests are an essential tool for collaborating in teams, as they ensure code quality, facilitate discussions about changes, and prevent errors from being introduced into the main project.
Next, we will explore how to use pull requests in larger, more complex projects involving multiple collaborators and remote branches.