While this question was asked a while ago and I don't know the state of the libraries at that point, it is worth mentioning for searchers that GitPython does a good job of abstracting the command line tools so that you don't need to use subprocess. There are some useful built in abstractions that you can use, but for everything else you can do things like:
import git
repo = git.Repo( '/home/me/repodir' )
print repo.git.status()
# checkout and track a remote branch
print repo.git.checkout( 'origin/somebranch', b='somebranch' )
# add a file
print repo.git.add( 'somefile' )
# commit
print repo.git.commit( m='my commit message' )
# now we are one commit ahead
print repo.git.status()
Everything else in GitPython just makes it easier to navigate. I'm fairly well satisfied with this library and appreciate that it is a wrapper on the underlying git tools.
UPDATE: I've switched to using the sh module for not just git but most commandline utilities I need in python. To replicate the above I would do this instead:
import sh
git = sh.git.bake(_cwd='/home/me/repodir')
print git.status()
# checkout and track a remote branch
print git.checkout('-b', 'somebranch')
# add a file
print git.add('somefile')
# commit
print git.commit(m='my commit message')
# now we are one commit ahead
print git.status()
Answer from underrun on Stack OverflowVideos
» pip install git-client
I've been working on a side project for a while and finally decided to share it with the community. Checkout pygitzen - a terminal-based Git client built entirely in Python, inspired by LazyGit.
What My Project Does
pygitzen is a TUI (Terminal User Interface) for Git repositories that lets you navigate commits, view diffs, track file changes, and manage branches - all without leaving your terminal. Think of it as a Python-native LazyGit.
Target Audience
I'm a terminal-first developer and love tools like htop, lazygit, and fzf. So this tool is made with such users in mind. Who loves TUI apps and wanted python solution for app like lazygit etc which can be used in times like where there is restriction to install any thing apart from python package or wanted something pure python based TUIs.
Comparison
Currently there is no pure python based TUI git client.
-
Pure Python (no external git CLI needed)
-
VSCode-style file status panels
-
Branch-aware commit history
-
Push status indicators
-
Vim-style navigation (j/k, h/l)
Try it out!
If you're a terminal-first developer who loves TUIs, give it a shot:
pip install pygitzen cd <your-git-repo> pygitzen
Feedback welcome!
This is my first PyPI package, so I'd love feedback on:
-
What features are missing?
-
What could be improved?
-
Is the UI intuitive?
-
Any bugs or issues?
Repo:
https://github.com/SunnyTamang/pygitzen
PyPI installation:
https://pypi.org/project/pygitzen/
Let me know what you think!
While this question was asked a while ago and I don't know the state of the libraries at that point, it is worth mentioning for searchers that GitPython does a good job of abstracting the command line tools so that you don't need to use subprocess. There are some useful built in abstractions that you can use, but for everything else you can do things like:
import git
repo = git.Repo( '/home/me/repodir' )
print repo.git.status()
# checkout and track a remote branch
print repo.git.checkout( 'origin/somebranch', b='somebranch' )
# add a file
print repo.git.add( 'somefile' )
# commit
print repo.git.commit( m='my commit message' )
# now we are one commit ahead
print repo.git.status()
Everything else in GitPython just makes it easier to navigate. I'm fairly well satisfied with this library and appreciate that it is a wrapper on the underlying git tools.
UPDATE: I've switched to using the sh module for not just git but most commandline utilities I need in python. To replicate the above I would do this instead:
import sh
git = sh.git.bake(_cwd='/home/me/repodir')
print git.status()
# checkout and track a remote branch
print git.checkout('-b', 'somebranch')
# add a file
print git.add('somefile')
# commit
print git.commit(m='my commit message')
# now we are one commit ahead
print git.status()
I thought I would answer my own question, since I'm taking a different path than suggested in the answers. Nonetheless, thanks to those who answered.
First, a brief synopsis of my experiences with GitPython, PyGit, and Dulwich:
- GitPython: After downloading, I got this imported and the appropriate object initialized. However, trying to do what was suggested in the tutorial led to errors. Lacking more documentation, I turned elsewhere.
- PyGit: This would not even import, and I could find no documentation.
- Dulwich: Seems to be the most promising (at least for what I wanted and saw). I made some progress with it, more than with GitPython, since its egg comes with Python source. However, after a while, I decided it may just be easier to try what I did.
Also, StGit looks interesting, but I would need the functionality extracted into a separate module and do not want wait for that to happen right now.
In (much) less time than I spent trying to get the three modules above working, I managed to get git commands working via the subprocess module, e.g.
def gitAdd(fileName, repoDir):
cmd = ['git', 'add', fileName]
p = subprocess.Popen(cmd, cwd=repoDir)
p.wait()
gitAdd('exampleFile.txt', '/usr/local/example_git_repo_dir')
This isn't fully incorporated into my program yet, but I'm not anticipating a problem, except maybe speed (since I'll be processing hundreds or even thousands of files at times).
Maybe I just didn't have the patience to get things going with Dulwich or GitPython. That said, I'm hopeful the modules will get more development and be more useful soon.
» pip install GitPython
I've seen GitPython, which looks like the most used one, but also dulwich and PyGit, and others. What are the pros/cons of these, and are there any others? Many modules I looked at seemed over complicated, and I've considered just dropping to subprocess since all I need to do is make some commits and push the changes to a remote