Create a Pull request on GitHub using Pycharm


What is GitHub?

GitHub is a well-known website that serves as a platform for hosting software projects and facilitating collaborative work on such projects. Git, which is a distributed version control system, may be used to manage changes to a codebase, which is one of the most important functions of the platform known as GitHub. Pycharm is a well-known integrated development environment (IDE) for Python, and it will be the tool that we will use to go through the process of creating a pull request on GitHub in this lesson.

Prerequisites

Before we start coding, there are a few prerequisites that you should have. Firstly, you need to have Python installed on your system. You can download and install Python from the official website. Secondly, you should have a code editor installed on your system. We recommend using Visual Studio Code or PyCharm.

Algorithm to Follow

We will start with a theoretical explanation of the code followed by a code example.

Installing of required libraries −You can install the GitPython library using pip, which is the Python package installer. Here are the steps to install GitPython −

Syntax

pip install GitPython

Once GitPython is installed, you can use it to interact with Git and the GitHub API. For example, you can use GitPython to clone a Git repository, commit changes to a repository, or create a pull request on GitHub. You can refer to the GitPython documentation for more information on how to use the library: https://gitpython.readthedocs.io/en/stable/

Step 1 − Fork the repository

Having forked the repository we want to contribute to, we can now make a pull request. When you fork a repository on GitHub, you make a clone of the original repository under your own account. Forking a repository is as simple as visiting the repository's GitHub website and clicking the "Fork" button.

# define the repository path and credentials
repo_path = '<path_to_forked_repository>'
username = '<GitHub_username>'
password = '<GitHub_password>'

Step 2 − Clone the forked repository

After creating a fork, you may clone the repository to work on it locally. Launch Pycharm, and then choose "Checkout from Version Control" from the Welcome to Pycharm screen or the File menu. Click "Clone" after entering the URL of your forked repository in the "Clone Repository" window.

# clone the forked repository
if not os.path.exists(repo_path):
   Repo.clone_from(f'https://{username}:{password}@github.com/{username}/{repo_name}.git', repo_path)

Step 3 − Create a new branch

Before making changes to the master codebase, it is best practise to fork off into a new branch first. This makes it simpler to monitor changes and keep them separate from the core code. Accessible via the "View" menu, Pycharm's "Git" tool window is where you'll go to create a new branch. Click the "Branches" button in the "Git" box after selecting your forked repository. Choose the "New Branch" option and provide a name for the new division.

# create a new branch
repo = Repo(repo_path)
new_branch = repo.create_head('<new_branch_name>')
new_branch.checkout()

Step 4 − Make changes and commit

When a new branch has been established, modifications to the codebase may be made. After you are satisfied with the results, it is time to commit the modifications. Choose "Version Control" from Pycharm's "View" menu to see the "Version Control" tool window, which you can then use to commit modifications. Choose your forked repository and hit "Commit" in the "Version Control" tool window. To save your changes, type a commit message and hit the "Commit" button.

Step 5 − Push the branch to GitHub

After making the necessary edits, the next step is to publish the branch to GitHub. Use the "Git" tool window in Pycharm and then choose your forked repository to push the branch. The "Push" button uploads the branch to GitHub.

# make changes and commit
# push the branch to GitHub
origin = repo.remote('origin')
origin.push(new_branch)

Step 6 − Create a pull request

A pull request can be made to incorporate the forked branch's modifications into the master repository after the branch has been uploaded to GitHub. In order to accomplish this, go to the GitHub repository you forked and choose the "New pull request" option. Choose the fork you wish to include into the master codebase, and then give your pull request a name and explanation. The pull request may be sent in by clicking the "Create pull request" button.

# create a pull request
response = origin.repo.create_pull(
   title='<pull_request_title>',
   body='<pull_request_body>',
   head='<new_branch_name>',
   base='<base_branch_name>'
)

Step 7 − Respond to feedback

When a change is made and submitted as a pull request, the project maintainers will examine it and offer feedback. You can implement this suggestion by committing new changes to the branch and releasing them to GitHub. The updated pull request will be generated mechanically.

Step 8 − Merge the pull request

If the maintainers of the project are satisfied with the modifications, they will include the pull request into the master codebase. You've made an important change to a GitHub project using Pycharm, so please accept our congratulations.

In conclusion, below are the actions required to initiate a pull request in Pycharm for GitHub −

  • Fork the repository

  • Clone the forked repository

  • Create a new branch

  • Make changes and commit

  • Push the branch to GitHub

  • Create a pull request

  • Respond to feedback

Example

In this section we will go through the code and perform the same steps using a python code.

# import required libraries
from git import Repo
import os

# define the repository path and credentials
repo_path = '<path_to_forked_repository>'
username = '<GitHub_username>'
password = '<GitHub_password>'

# clone the forked repository
if not os.path.exists(repo_path):
   Repo.clone_from(f'https://{username}:{password}@github.com/{username}/{repo_name}.git', repo_path)
# create a new branch
repo = Repo(repo_path)
new_branch = repo.create_head('<new_branch_name>')
new_branch.checkout()
# make changes and commit
# push the branch to GitHub
origin = repo.remote('origin')
origin.push(new_branch)
# create a pull request
response = origin.repo.create_pull(
   title='<pull_request_title>',
   body='<pull_request_body>',
   head='<new_branch_name>',
   base='<base_branch_name>'
)

Some points to remember − you will need to replace <path_to_forked_repository>, <GitHub_username>, <GitHub_password>, <new_branch_name>, <pull_request_title>, <pull_request_body>, <base_branch_name>, and the code for making changes and committing with your own values and code.

Also note that this example uses the GitPython library to interact with Git and the GitHub API. You will need to install this library using pip or another package manager before running this code.

Conclusion

This article has taught us how to utilise the Python IDE PyCharm and the version control system Git by issuing pull requests and creating forks of existing repositories to tailor them to our requirements. Python is a flexible programming language used in all kinds of different fields to make our lives easier and our output greater.

Updated on: 21-Dec-2023

45 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements