This is a silly repository. If you want to control a Git repository from Python, you should use GitPython.
If however, you were just going to run shell commands from Python then save yourself the hassle of writing it and use this. It is particularly useful for prototyping applications that need to interact with Git.
GitPython is just a collection of functions that will execute shell commands to manage the repository.
- Git
Since PythonGit merely executes shell commands to control the repository, it must be installed on your system for this library to work.
Download the source code from Github and run setup.py
Import the git module, or specifically import the Repository class therefrom.
from git import Repository
Create an instance of the Repository class, providing the path to the repository. This path will be run through os.path.abspath and used as the cwd for git commands.
repo = Repository("/path/to/repository/")
Repositories contain the following methods.
All the methods listed here return tuples containing the resulting STDOUT and STDERR. Additionally, the instance's out and err properties will always contain the STDOUT and STDERR respectively from the last method called.
repo.cmd(command, *args)
This is the method used internally by the specific git command methods. It is a flexible interface to the git repository. If you need to use a git command that does not have a method built-in you can use this directly. The first argument is the git command to run and subsequent arguments are passed to it.
All the other commands provided are just helper methods for calling this one with the right arguments. The rest aren't useless because I think they'll be a bit easier to understand in your code (since they look more like their Git counterparts).
repo.add(filename, *args)
The add method runs git add <filename> [args1, ... ] in the repository.
repo.commit(message, author, *args)
This method runs git commit -m <message> --author=<author> [args1, ... ] in the repository.
repo.status(*args)
This is a method to run git status [args1, ... ] in the repository.
repo.checkout(checkout, *args)
The checkout method runs git checkout [args1, ... ] in the repository.
repo.pull(remote, branch, *args)
This method runs git pull <remote> <branch> [args1, ... ] in the repository.
repo.push(remote, branch, *args)
The push method runs git push <remote> <branch> [args1, ... ] in the repository.
repo.fetch(remote, *args)
The fetch method runs git fetch <remote> [args1, ... ] in the repository.
repo.merge(ref, *args)
The merge method runs git merge <ref> [args1, ... ] in the repository.