Pull Request with GitHub

A pull request is a collaborative tool used to propose changes made on a separate branch for review and merging. The workflow involves forking (for external repositories), creating a branch, making changes, pushing the branch, and creating a pull request. Once the pull request is approved, the changes can be merged into the main branch, simplifying collaboration in teams and open-source projects.

Bernd Fischer

Bernd Fischer

13. Oct 2024 · 3 min read

Pull Request with GitHub

A pull request (PR) is a method to propose changes made on a branch of a repository for review and integration into another branch (usually the main branch). Pull requests are a central feature of the collaborative workflow in Git and GitHub, especially in open-source projects or teams working on different features of a project.

1. Prerequisites

Before you can create a pull request, some basic steps need to be completed:

  1. GitHub Account: You need a GitHub account to collaborate on a repository.
  2. Repository (Repo): A repository is a project you are working on. It can be your own or someone else's.
  3. Git Installed: Git should be installed on your local system to make changes locally and push them.

2. Workflow for a Pull Request

The usual process for a pull request is as follows:

  1. Fork the repository (for external projects)
  2. Create a branch for your changes
  3. Make changes and commit on the new branch
  4. Push the branch to GitHub
  5. Create a pull request and send it for review
  6. Review and discussion by the team or maintainer
  7. Merge the pull request into the main branch

3. Detailed Steps

Step 1: Fork the repository (optional)

If you want to contribute to a repository you don't own, you must first fork it. Forking creates a copy of the repository under your GitHub account.

  1. Navigate to the repository on GitHub.
  2. Click the Fork button at the top right.
  3. The repository is copied to your GitHub account.

Step 2: Clone the repository

Cloning means copying the repository locally to your computer so you can make changes.

git clone https://github.com/DEIN_BENUTZERNAME/REPOSITORY_NAME.git

Change into the cloned directory:

cd REPOSITORY_NAME

Step 3: Create a new branch

It's best practice to make changes on a separate branch to keep the main branch clean and stable.

Create a new branch and switch to this branch:

git checkout -b feature-branch

Here, feature-branch stands for the new branch name. Choose a name that describes what you intend to do (e.g., bugfix, new-feature, etc.).

Step 4: Make changes and commit

Make the necessary changes to the code, save your files, and add them to the Git staging area:

git add .

Then create a commit with a meaningful message:

git commit -m "Fügt neues Feature hinzu"

Step 5: Push the branch

Push your new branch to GitHub:

git push origin feature-branch

Now your new branch is uploaded to your GitHub repository.


Step 6: Create a pull request

Once your branch is pushed to GitHub, you can create a pull request.

  1. Go to your repository on GitHub.
  2. You will see a message indicating you've recently pushed a branch, with a button labeled "Compare & Pull Request."
  3. Click Compare & Pull Request.

This opens the form to create a pull request. In this form:

  • Select the base branch you want to merge your changes into (usually main or master).
  • Select your feature branch that you just created.
  • Write a description of your pull request. Here you should explain what your changes do, why they are necessary, and how they can be tested.

Step 7: Review and discussion

After the pull request is created, other contributors, team members, or the repository maintainer can review the code.

  • There may be comments suggesting changes or pointing out errors.
  • In this case, you can further improve your code and add new commits to your branch. These changes will automatically be included in the existing pull request.

Step 8: Merge the pull request

Once your pull request is approved, the maintainer or you (depending on permissions) can merge the pull request into the main branch. This merges the changes from your branch into the main branch.

If your pull request has conflicts with the main branch, these must be resolved manually before the merge can occur.


4. Example of a pull request

Suppose you updated a README.md in a project and want to submit your changes for review.

Steps:

  1. Fork the repository on GitHub (optional).
  2. Clone the repository: bash git clone https://github.com/DEIN_BENUTZERNAME/REPOSITORY_NAME.git cd REPOSITORY_NAME

  3. Create a new branch: bash git checkout -b update-readme

  4. Make changes: Modify the content of the README.md file.

  5. Commit changes: bash git add README.md git commit -m "Aktualisiere das README"

  6. Push branch: bash git push origin update-readme

  7. Create pull request: Go to your GitHub repository and click “Compare & Pull Request.”

  8. Add a meaningful description and submit the pull request.