Have you considered using GitPython? It's designed to handle all this nonsense for you.

import git  # pip install gitpython


g = git.cmd.Git(git_dir)
g.pull()

To install the module use pip install GitPython

Project can be found here, GitPython

Answer from jleahy on Stack Overflow
🌐
Readthedocs
gitpython.readthedocs.io › en › stable › tutorial.html
GitPython Tutorial — GitPython 3.1.46 documentation
from git import Repo # rorepo is a Repo instance pointing to the git-python repository. # For all you know, the first argument to Repo is a path to the repository you # want to work with.
🌐
AskPython
askpython.com › home › how to use gitpython to pull remote repository?
How To Use GitPython To Pull Remote Repository? - AskPython
June 30, 2023 - repo.git.pull() is executing the “git pull” command on the local repository using the repo object. Your local repository will now have all the updates from the git repository. Now open your command line and run your Python program to see the results.
🌐
Python Developer's Guide
devguide.python.org › gitbootcamp
Git bootcamp and cheat sheet
You should have been redirected · If not, click here to continue
🌐
PyPI
pypi.org › project › git-pull-request
git-pull-request · PyPI
Python :: 3.10 · Report project as malware · git-pull-request is a command line tool to send GitHub or Pagure pull-request from your terminal. Use the standard Python installation method: pip3 install git-pull-request · Although it might ...
      » pip install git-pull-request
    
Published   Jan 04, 2022
Version   6.0.2
🌐
GeeksforGeeks
geeksforgeeks.org › python › automating-some-git-commands-with-python
Automating some git commands with Python - GeeksforGeeks
April 28, 2025 - Python3 · import git repo = git.Repo('/home/hardik/GFG_Temp/Cloned_Repo') # Select an existing branch existing_branch = repo.heads['existing_branch'] existing_branch.checkout() print('Branch Changed to an existing branch') Output: Branch Changed to an existing branch · To update the local repository with the latest changes from the remote repository we use git pull command ·
🌐
Medium
zchelseal.medium.com › automate-your-git-workflow-with-gitpython-3320e833c4e2
Automate Your Git Workflow with GitPython | by Chelsea Liu | Medium
April 13, 2021 - Construct a url that leads us to a PR-creation page (this method is hacky but safe for as long as GitHub keeps this convention): "https://{}/pull/new/{}".format(link_to_your_github_repo, your_branch_name)
🌐
GitHub
gist.github.com › romach › 27a4c3dee97d9bf2213c6c3b379fd7e4
Git pull from Python · GitHub
Git pull from Python. GitHub Gist: instantly share code, notes, and snippets.
Find elsewhere
🌐
Python Snacks
pythonsnacks.com › p › using-git-push-and-git-pull-for-your-python-code
[DELETE] Using Git Push and Git Pull for your Python Code
April 14, 2025 - Now, your team can see all of the changes that you made to your button. Similarly, if your co-worker pushed up a change that is a separate from your work on the button, you can run the git pull command to pull their changes to your local machine. Want even more Python-related content that’s useful?
🌐
GitHub
github.com › BenjaFriend › AutoPull
GitHub - BenjaFriend/AutoPull: A simple python script to automatically pull any repos in the containing folder. Specifically useful for those using Git for school assignments and need to grade.
April 12, 2022 - A simple python script to automatically pull any repos in the containing folder. Specifically useful for those using Git for school assignments and need to grade. - GitHub - BenjaFriend/AutoPull: A simple python script to automatically pull ...
Author   BenjaFriend
🌐
PDX
web.pdx.edu › ~gjay › teaching › mth271_2020 › html › 03_Working_with_git.html
Git Repo class in python
The code below uses the conditionals if and else (included in the prerequisite reading for this lecture) to check if the folder exists: If it does not exist, a new local copy of the GitHub repository is cloned into your local hard drive. If it exists, then only the differences (or updates) between your local copy and the remote repository are fetched, so that your local copy is up to date with the remote. ... if os.path.isdir(repodir): # if repo exists, pull newest data repo = Repo(repodir) repo.remotes.origin.pull() else: # otherwise, clone from remote repo = Repo.clone_from('https://github.com/jayggg/mth271content', repodir)
🌐
GitHub
github.com › msiemens › PyGitUp
GitHub - msiemens/PyGitUp: A nicer `git pull`
The original git-up has been written by aanand: aanand/git-up/. Switch to packaging instead of pkg_resources. Closes #139. Fix top-level directory detection on MinGW. Update dependencies and switch to the uv package manager. Improve logging when updating large repositories. Thanks @bdmartin for Pull Request #132. ... Add support for Python 3.11.
Starred by 537 users
Forked by 45 users
Languages   Python 100.0% | Python 100.0%
🌐
GitHub
github.com › liferay › git-tools › blob › master › git-pull-request › git-pull-request.py
git-tools/git-pull-request/git-pull-request.py at master · liferay/git-tools
#!/usr/bin/env python3 · # -*- coding: utf-8 -*- · """ Git command to automate many common tasks involving pull requests. · Usage: · gitpr [<options>] <command> [<args>] · Options: · -h, --help · Display this message. · -r <repo>, --repo <repo> Use this github repo instead of the 'remote origin' or 'github.repo' git config setting.
Author   liferay
🌐
DevDungeon
devdungeon.com › content › working-git-repositories-python
Working with Git Repositories in Python | DevDungeon
March 17, 2020 - The GitPython project allows you to work in Python with Git repositories. In this guide we'll look at some basic operations like: Initializing a repo · Cloning a repo · Adding and committing · Pushing and pulling with remotes · Checking for changes · Getting a diff ·
🌐
GitHub
github.com › gitpython-developers › GitPython
GitHub - gitpython-developers/GitPython: GitPython is a python library used to interact with Git repositories. · GitHub
Style and formatting checks, and running tests on all the different supported Python versions, will be performed: Upon submitting a pull request. On each push, if you have a fork with GitHub Actions enabled.
Starred by 5.1K users
Forked by 968 users
Languages   Python
🌐
DNMTechs
dnmtechs.com › calling-git-pull-from-within-python
Calling ‘git pull’ from within Python – DNMTechs – Sharing and Storing Technology Knowledge
The ‘subprocess.call()’ function takes a list of arguments, where the first element is the command to be executed (‘git’) and the subsequent elements are the command-line arguments (‘pull’). By calling this function, we can trigger a ‘git pull’ operation from within our Python script.
Top answer
1 of 3
9

GitPython is only a wrapper around Git. I assume you are wanting to create a pull request in a Git hosting service (Github/Gitlab/etc.).

You can't create a pull request using the standard git command line. git request-pull, for example, only Generates a summary of pending changes. It doesn't create a pull request in GitHub.

If you want to create a pull request in GitHub, you can use the PyGithub library.

Or make a simple HTTP request to the Github API with the requests library:

import json
import requests

def create_pull_request(project_name, repo_name, title, description, head_branch, base_branch, git_token):
    """Creates the pull request for the head_branch against the base_branch"""
    git_pulls_api = "https://github.com/api/v3/repos/{0}/{1}/pulls".format(
        project_name,
        repo_name)
    headers = {
        "Authorization": "token {0}".format(git_token),
        "Content-Type": "application/json"}

    payload = {
        "title": title,
        "body": description,
        "head": head_branch,
        "base": base_branch,
    }

    r = requests.post(
        git_pulls_api,
        headers=headers,
        data=json.dumps(payload))

    if not r.ok:
        print("Request Failed: {0}".format(r.text))

create_pull_request(
    "<your_project>", # project_name
    "<your_repo>", # repo_name
    "My pull request title", # title
    "My pull request description", # description
    "banana-refresh", # head_branch
    "banana-integration", # base_branch
    "<your_git_token>", # git_token
)

This uses the GitHub OAuth2 Token Auth and the GitHub pull request API endpoint to make a pull request of the branch banana-refresh against banana-integration.

2 of 3
3

It appears as though pull requests have not been wrapped by this library.

You can call the git command line directly as per the documentation.

repo.git.pull_request(...)