git-python (pynickle)
git-python by pynickle is a lightweight Python wrapper that simplifies a small subset of Git commands for personal use. It functions primarily as a command-line tool, providing simplified commands like `init`, `add`, and `commit`. It is not the widely-used `GitPython` library (note the capitalization). The current version is 1.1.2, with an irregular release cadence focused on personal utility.
Warnings
- gotcha There is a significant naming collision: `git-python` (this library, by pynickle) vs. `GitPython` (a comprehensive and widely-used library for programmatic Git interaction by gitpython-developers). They are distinct projects with different APIs and functionalities.
- gotcha This `git-python` library is primarily designed as a command-line tool, not for direct programmatic imports. Its internal `GitPython` class methods (e.g., `init`, `add`) often rely on the script's execution environment or `os.path.dirname(__file__)` to determine paths, making them unsuitable for managing arbitrary Git repositories programmatically from a different script context.
- gotcha The `git-python` library (pynickle) provides a very limited subset of Git commands, including only `init`, `add`, and `commit`. It is not intended as a full-featured Git client or wrapper.
Install
-
pip install git-python
Imports
- GitPython
from gitpython.gp import GitPython
Quickstart
import subprocess
import os
import tempfile
import shutil
# Create a temporary directory for the Git repo
temp_dir = tempfile.mkdtemp()
print(f"Created temporary directory: {temp_dir}")
original_cwd = os.getcwd()
try:
os.chdir(temp_dir)
# 1. Initialize a new gitpython repository
print("Initializing gitpython repository...")
result = subprocess.run(["gitpython", "gp", "init"], capture_output=True, text=True, check=True)
print(f"init stdout:\n{result.stdout}")
# 2. Create a file
with open("example.txt", "w") as f:
f.write("Hello, git-python!")
print("Created example.txt")
# 3. Add the file
print("Adding example.txt...")
result = subprocess.run(["gitpython", "gp", "add", "."], capture_output=True, text=True, check=True)
print(f"add stdout:\n{result.stdout}")
# 4. Commit the file
print("Committing changes...")
result = subprocess.run(["gitpython", "gp", "commit", "-m", "Initial commit via git-python CLI"], capture_output=True, text=True, check=True)
print(f"commit stdout:\n{result.stdout}")
# Verify using standard git CLI (optional, but good for demo)
print("\nVerifying with standard 'git' command:")
result = subprocess.run(["git", "log", "--oneline"], capture_output=True, text=True, check=True)
print(f"git log stdout:\n{result.stdout}")
except subprocess.CalledProcessError as e:
print(f"Error during subprocess call: {e}")
print(f"Stdout: {e.stdout}")
print(f"Stderr: {e.stderr}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
finally:
os.chdir(original_cwd)
# Clean up the temporary directory
print(f"Cleaning up temporary directory: {temp_dir}")
shutil.rmtree(temp_dir)