it's all in the documentation:

repo = Repo.clone_from(cloneUrl, localRepopath)
remote = repo.create_remote(remote_name, url=another_url)
remote.push(refspec='{}:{}'.format(local_branch, remote_branch))

see also the push reference API. You can avoid the refspec setting if you set a tracking branch for the remote you want to push to.

Answer from zmo on Stack Overflow
🌐
GitHub
github.com › gitpython-developers › GitPython › issues › 471
Create a new branch and push to remote? · Issue #471 · gitpython-developers/GitPython
June 13, 2016 - Hi, i really tried reading all over the docs, and tutorial but for some reason i can't find it. given repo - how can i create a new local branch and push it to remote? i must say even i really like the API i think a simple demonstration ...
Author   itielshwartz
🌐
Readthedocs
gitpython.readthedocs.io › en › stable › tutorial.html
GitPython Tutorial — GitPython 3.1.46 documentation
Remotes allow to handle fetch, pull and push operations, while providing optional real-time progress information to progress delegates.
🌐
AskPython
askpython.com › home › how to use gitpython to pull remote repository?
How To Use GitPython To Pull Remote Repository? - AskPython
June 30, 2023 - Push: In GitPython, you can push your local changes to the remote repository using the push() method.
🌐
GitHub
github.com › gitpython-developers › GitPython › issues › 567
Push a commit to a named branch · Issue #567 · gitpython-developers/GitPython
I've been reading through the documentation, and I'm probably missing something, but my question is how would I achieve the following through this library? I'm trying to do the equivalent of this: git push origin master:some-remote-branch
🌐
Medium
zchelseal.medium.com › automate-your-git-workflow-with-gitpython-3320e833c4e2
Automate Your Git Workflow with GitPython | by Chelsea Liu | Medium
April 13, 2021 - Don’t forget to import git at the top of your script (sidenote: if you’ve also wondered why the package names (GitPython) are sometimes different from their import counterparts (git), here are some insights from wonderful internet people). Grab your repo with the latest meta-data from origin: repo = git.Repo(path_to_your_repo) # ex. "/User/some_user/some_dir" origin = repo.remote("origin") assert origin.exists() origin.fetch()
🌐
Readthedocs
gitpython.readthedocs.io › en › 0.3.2 › tutorial.html
GitPython Tutorial — GitPython 0.3.2 documentation
Remotes are used as alias for a foreign repository to ease pushing to and fetching from them:
Top answer
1 of 4
3

I'm using gitpython==2.1.11 with Python 3.7. Below is my push function in which I first try a high-level push, and then a low-level push as necessary. Note how I check the return value of either command. I also log the push actions, and this explains what's happening at every step.

class GitCommandError(Exception):
    pass

class Git:
    def _commit_and_push_repo(self) -> None:
        repo = self._repo
        remote = repo.remote()
        remote_name = remote.name
        branch_name = repo.active_branch.name

        # Note: repo.index.entries was observed to also include unpushed files in addition to uncommitted files.
        log.debug('Committing repository index in active branch "%s".', branch_name)
        self._repo.index.commit('')
        log.info('Committed repository index in active branch "%s".', branch_name)

        def _is_pushed(push_info: git.remote.PushInfo) -> bool:
            valid_flags = {push_info.FAST_FORWARD, push_info.NEW_HEAD}  # UP_TO_DATE flag is intentionally skipped.
            return push_info.flags in valid_flags  # This check can require the use of & instead.

        push_desc = f'active branch "{branch_name}" to repository remote "{remote_name}"'
        log.debug('Pushing %s.', push_desc)
        try:
            push_info = remote.push()[0]
        except git.exc.GitCommandError:  # Could be due to no upstream branch.
            log.warning('Failed to push %s. This could be due to no matching upstream branch.', push_desc)
            log.info('Reattempting to push %s using a lower-level command which also sets upstream branch.', push_desc)
            push_output = repo.git.push('--set-upstream', remote_name, branch_name)
            log.info('Push output was: %s', push_output)
            expected_msg = f"Branch '{branch_name}' set up to track remote branch '{branch_name}' from '{remote_name}'."
            if push_output != expected_msg:
                raise RepoPushError(f'Failed to push {push_desc}.')
        else:
            is_pushed = _is_pushed(push_info)
            logger = log.debug if is_pushed else log.warning
            logger('Push flags were %s and message was "%s".', push_info.flags, push_info.summary.strip())
            if not is_pushed:
                log.warning('Failed first attempt at pushing %s. A pull will be performed.', push_desc)
                self._pull_repo()
                log.info('Reattempting to push %s.', push_desc)
                push_info = remote.push()[0]
                is_pushed = _is_pushed(push_info)
                logger = log.debug if is_pushed else log.error
                logger('Push flags were %s and message was "%s".', push_info.flags, push_info.summary.strip())
                if not is_pushed:
                    raise RepoPushError(f'Failed to push {push_desc} despite a pull.')
        log.info('Pushed %s.', push_desc)
2 of 4
1

You have to define a remote repo, then push to it. e.g.

origin = repo.remote(name='origin')
origin.push()

See the Handling Remotes documentation for more examples of push/pull

Find elsewhere
🌐
GitHub
github.com › gitpython-developers › GitPython › issues › 334
Is there a way to force a git push? · Issue #334 · gitpython-developers/GitPython
August 5, 2015 - Hi guys, I'm using GitPython to commit and push some changes in my Python application. However, sometimes my developers and I want to force push the commit (git push --force) instead of sometimes the app failing because it's behind the H...
Author   g12mcgov
🌐
Readthedocs
gitpython.readthedocs.io › en › stable › reference.html
API Reference — GitPython 3.1.46 documentation
This only works if we have a local tracking branch, which is the case if the remote repository had a master branch, or if the branch option was specified for this submodule and the branch existed remotely. progress – UpdateProgress instance, or None if no progress should be shown. dry_run – If True, the operation will only be simulated, but not performed. All performed operations are read-only. ... If True, we may reset heads even if the repository in question is dirty. Additionally we will be allowed to set a tracking branch which is ahead of its remote branch back into the past or the location of the remote branch.
🌐
Grimoire
grimoire.carcano.ch › the grimoire of a modern linux professional › blog › dev-ops tools › scripting › git with python howto gitpython tutorial and pygit2 tutorial
Git With Python HowTo GitPython Tutorial And PyGit2 Tutorial
November 14, 2024 - Git supports two types of tags: lightweight and annotated. GitPython of course support both kind of tags. ... A very common use case is pushing to the remote Git repository the committed changes.
🌐
GitHub
github.com › gitpython-developers › GitPython › blob › master › git › remote.py
GitPython/git/remote.py at master · gitpython-developers/GitPython
# see: https://github.com/gitpython-developers/GitPython/pull/528#issuecomment-252976319 · # and: http://stackoverflow.com/a/32991784/548792 · # if 'Unknown subcommand: get-url' in str(ex): try: remote_details = self.repo.git.remote("show", self.name) for line in remote_details.split('\n'): if ' Push URL:' in line: yield line.split(': ')[-1] except GitCommandError as ex: if any(msg in str(ex) for msg in ['correct access rights', 'cannot run ssh']): # If ssh is not setup to access this repository, see issue 694 ·
Author   gitpython-developers
🌐
LinuxConfig
linuxconfig.org › home › how to manage git repositories with python
Manage Git Repositories with Python: GitPython Guide
May 27, 2022 - What we did above is calling the push method and pass a mapping between the local branch and the remote one as first argument: we basically sad to push the content of our master branch to the remote master branch.
🌐
Readthedocs
gitpython.readthedocs.io › en › 3.1.26 › tutorial.html
GitPython Tutorial — GitPython 3.1.26 documentation
Remotes allow to handle fetch, pull and push operations, while providing optional real-time progress information to progress delegates.
Top answer
1 of 3
14

I've done something like creating a txt in a remote branch from newly created branch and commit, push to remote. Here's my code

import git
import datetime
import os
from time import *
from os import path
from git import Repo

def commit_files():
    if repo != None:
        new_branch = 'your_new_branch'
        current = repo.create_head(new_branch)
        current.checkout()
        master = self.repo.heads.master
        repo.git.pull('origin', master)
        #creating file
        dtime = strftime('%d-%m-%Y %H:%M:%S', localtime())
        with open(self.local_repo_path + path.sep + 'lastCommit' + '.txt', 'w') as f:
            f.write(str(dtime))
        if not path.exists(self.local_repo_path):
            os.makedirs(self.local_repo_path)
        print('file created---------------------')

        if repo.index.diff(None) or repo.untracked_files:

            repo.git.add(A=True)
            repo.git.commit(m='msg')
            repo.git.push('--set-upstream', 'origin', current)
            print('git push')
        else:
            print('no changes')
2 of 3
1

This was exactly my use case, with the added requirement that I needed to switch back to original branch afterwards. I did it using this:

def switch_commit_branch(branch_name, msg='Automatic commit by switch_commit_branch', force=True):
    '''
    This function 
        (1) switches to branch `branch_name` (and creates it if it doesnt aready exist)
        (2) ads and commits all changes
        (3) pushes to origin
    :param branch_name: desired branch name
    :param msg: commit message
    :param force: whether to create branch if it doesnt already exist 
    :return: 
    '''
    repo = Repo(search_parent_directories=True)
    original_branch = repo.active_branch
    branch = [b for b in repo.branches if b.name == branch_name]
    if len(branch) == 0:
        if force:
            print(f'Creating new branch {branch_name}')
            branch = repo.create_head(branch_name)  # create new branch if none exists
        else:
            print(f'Branch {branch_name} does not exist. Set `force=True` to create. Aborting')
            return None
    else:
        branch = branch[0]

    branch.checkout()
    repo.git.add('*')
    repo.git.commit(m=msg)
    repo.git.push('--set-upstream', 'origin', branch)
    original_branch.checkout()
🌐
Python Developer's Guide
devguide.python.org › gitbootcamp
Git bootcamp and cheat sheet
You should have been redirected. If not, click here to continue.
🌐
GeeksforGeeks
geeksforgeeks.org › python › automating-some-git-commands-with-python
Automating some git commands with Python - GeeksforGeeks
April 28, 2025 - To update the local repository with the latest changes from the remote repository we use git pull command
🌐
DevDungeon
devdungeon.com › content › working-git-repositories-python
Working with Git Repositories in Python | DevDungeon
March 17, 2020 - The [GitPython](https://gitpython.readthedocs.io/) 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 - Listing and switching branches For full documentation, see [https://gitpython.readthedocs.io/](https://gitpython.readthedocs.io/). The source code is available at [https://github.com/gitpython-developers/GitPython](https://github.com/gitpython-developers/GitPython).