Following is the code to git add, git commit and then git push using GitPython.

Install GitPython using pip install gitpython.

from git import Repo

PATH_OF_GIT_REPO = r'path\to\your\project\folder\.git'  # make sure .git folder is properly configured
COMMIT_MESSAGE = 'comment from python script'

def git_push():
    try:
        repo = Repo(PATH_OF_GIT_REPO)
        repo.git.add(update=True)
        repo.index.commit(COMMIT_MESSAGE)
        origin = repo.remote(name='origin')
        origin.push()
    except:
        print('Some error occured while pushing the code')    

git_push()
Answer from Niladri Basu on Stack Overflow
🌐
JetBrains
jetbrains.com › help › pycharm › commit-and-push-changes.html
Commit and push changes to Git repository | PyCharm Documentation
February 16, 2026 - When you're ready, click Commit or Commit and Push (Ctrl+Alt+K) to push the changes to the remote repository immediately after the commit. You will be able to review the current commit as well as all other commits before they are pushed to the ...
Discussions

git push using python - Stack Overflow
Get early access and see previews of new features. Learn more about Labs ... I have local git repository. I am using python to commit the local repo using gitpython library. I want to push the commit to github. How can I do this using gitpython or any other library. More on stackoverflow.com
🌐 stackoverflow.com
April 3, 2020
I made a Python script that checks the commit and push status of my important git repositories.
git status --short gives output that is probably easier to parse and also more robust: https://git-scm.com/docs/git-status#_short_format More on reddit.com
🌐 r/git
5
14
January 11, 2020
Is there a way to force a git push?
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 sometim... More on github.com
🌐 github.com
2
August 5, 2015
how do I commit and push to github from python shell? - Stack Overflow
I have three .py files saved in the python shell/IDLE. I would like to commit and push them to my GitHub account/repo. Is this possible? More on stackoverflow.com
🌐 stackoverflow.com
🌐
Readthedocs
gitpython.readthedocs.io › en › stable › tutorial.html
GitPython Tutorial — GitPython 3.1.46 documentation
The update method will track changes ... tree and submodule checkouts stay consistent, which is very useful in case submodules get deleted or added to name just two of the handled cases. Additionally, GitPython adds functionality to track a specific branch, instead of just a commit...
🌐
Medium
zchelseal.medium.com › automate-your-git-workflow-with-gitpython-3320e833c4e2
Automate Your Git Workflow with GitPython | by Chelsea Liu | Medium
April 13, 2021 - Push this branch to origin along with your new commit(s): repo.git.push("--set-upstream", origin, repo.head.ref) 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) There are other ways to create a pull request as well, one of which is to use the PyGithub library to authenticate with a GitHub token and publish a pull request on behalf of whichever account this token belongs to.
🌐
GeeksforGeeks
geeksforgeeks.org › python › automating-some-git-commands-with-python
Automating some git commands with Python - GeeksforGeeks
4 days ago - Uses Python modules like subprocess or libraries such as GitPython · Helps automate repetitive tasks like commits, pulls, and pushes
🌐
Kansas State University
textbooks.cs.ksu.edu › cc410 › z-examples › 01-hello-real-world › 04-python › 03-git-commit-push
Git Commit & Push :: CC 410 Textbook
June 17, 2024 - # check that the correct files are added git status # update the commit message below git commit -m "Commit Message Here" git push That will commit and push your changes to GitHub, which can now be found in the repository for this assignment.
🌐
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
Find elsewhere
🌐
Python Developer's Guide
devguide.python.org › gitbootcamp
Git bootcamp and cheat sheet
You should have been redirected · If not, click here to continue
🌐
LinuxConfig
linuxconfig.org › home › how to manage git repositories with python
Manage Git Repositories with Python: GitPython Guide
May 27, 2022 - In this tutorial we learned how to start working with git repositories using Python and the GitPython library. We saw how to clone or initialize a repository, how to add remotes, how to create commits and how to push and pull to and from the remote. We also saw how to check if a repository has changes and how to manage its submodules.
🌐
Reddit
reddit.com › r/git › i made a python script that checks the commit and push status of my important git repositories.
r/git on Reddit: I made a Python script that checks the commit and push status of my important git repositories.
January 11, 2020 -

This script isn't perfect or amazing. It parses git's output for hardcoded english strings instead of using a library, and the directories are hardcoded instead of in a config file. But maybe you will like this idea and expand on it in your own way. Figured I may as well share it rather than not.

https://github.com/voussoir/cmd/blob/master/gitcheckup.py

The output looks like this:

[ ][P] D:\Git\cmd (+0, -0, ~1)
[ ][ ] D:\Git\epubfile (+0, -0, ~1)
[ ][ ] D:\Git\Etiquette (+0, -0, ~4)
[ ][P] D:\Git\reddit (+3, -0, ~45)
[ ][P] D:\Git\reddit\Timesearch (+0, -0, ~1)
[C][P] D:\Git\sigilplugins
[ ][P] D:\Git\voussoirkit (+1, -0, ~0)
[C][P] D:\Git\YCDL

[C] means everything has been committed, [P] means the latest commit has been pushed, even if there are yet more uncommitted changes.

It uses my winwhich module (pip install voussoirkit) because my PATH contains a .lnk to git instead of the actual exe. If you don't want that, you can swap it out for shutil.which or hardcode your git executable or whatever.

Have fun!

🌐
Plain English Westminster
benhoyt.com › writings › pygit
pygit: Just enough of a Git client to create a repo, commit, and push itself to GitHub
pygit implements just enough of a Git client (in 500 lines of Python) to create a repo, commit, and push itself to GitHub.
🌐
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
🌐
Medium
medium.com › @tzuni_eh › git-commit-with-gitpython-and-ssh-key-926cad91ca89
Git commit with gitpython and SSH key | by Eva(Tzuni) Hsieh | Medium
January 5, 2019 - This is a very basic Python Git class which gives you an idea about how to use gitpython library to clone/commit/add/push files to your Git repository.
🌐
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 - Pull - Synchronize what’s in GitHub with your project on your computer. When you “push” code, you’re taking all of your commits (“snapshots”) and sending them to GitHub.
🌐
Waylon Walker
waylonwalker.com › python-git
Using Git from Python - Waylon Walker
April 30, 2022 - >>> print(repo.git.status()) On branch main Your branch is ahead of 'origin/main' by 1 commit. (use "git push" to publish your local commits) Untracked files: (use "git add <file>..." to include in what will be committed) blog/
🌐
Python GUIs
pythonguis.com › tutorials › getting started with git and github in your python projects
Git and GitHub for Python Projects: A Beginner's Guide to Version Control
March 20, 2023 - Learn how to use Git and GitHub for version control in your Python projects. This step-by-step guide covers installing Git, creating repositories, branching, merging, using .gitignore, and pushing code to GitHub with SSH. Using a