A Python implementation of Git that demonstrates the core concepts and internals of version control systems. This project is to understand how Git works under the hood by implementing the fundamental data structures and operations.
- Base class for all Git objects (Blob, Tree, Commit)
- Handles serialization/deserialization with zlib compression
- Generates SHA-1 hashes for object identification (real Git uses SHA-256 nowadays)
- Store actual file contents
- Represent individual files in the repository
- Represent directory structures
- Store references to blobs and other trees
- Maintain file permissions and names
- Store metadata about commits (author, timestamp, message)
- Reference tree objects and parent commits
- Form the commit history chain
- Manages the
.gitdirectory structure - Handles object storage and retrieval
- Implements Git commands (init, add, commit, checkout, etc.)
- Repository Initialization - Create new Git repositories
- File Staging - Add files to the staging area
- Commit Creation - Create commits with messages and metadata
- Branch Management - Create, switch, and delete branches
- Commit History - View commit logs and history
- Status Checking - Monitor repository state
- Object Storage - Efficient storage using SHA-1 hashing and compression
- Python 3.7+
- No external dependencies required (uses standard libraries only)
python3 main.py init
# Output: Initialized empty Git repository in ./.git# Add single file
python3 main.py add main.py
# Add entire directory
python3 main.py add src/
# Add multiple files
python3 main.py add file1.py file2.py src/python3 main.py commit -m "Add new feature"
python3 main.py commit -m "Fix bug" --author "Rivaan Ranawat <rivaan@rivaan.com>"# List branches
python3 main.py branch
# Create new branch
python3 main.py checkout -b feature-branch
# Switch to existing branch
python3 main.py checkout main
# Delete branch
python3 main.py branch feature-branch -d# Check working directory status
python3 main.py status
# View commit history
python3 main.py log -n 5build-git-python/
├── main.py # Main PyGit implementation
├── README.md # This file
└── .git/ # Git repository (created after init)
├── objects/ # Git objects database
├── refs/ # References and branches
├── HEAD # Current branch pointer
└── index # Staging area
- Files are stored as Blob objects with compressed content
- Directories are represented as Tree objects with file references
- Each object gets a unique SHA-1 hash
- Files are read and converted to Blob objects
- Object hashes are stored in the index (staging area)
- Index tracks which files are ready for commit
- Creates a Tree object from the current index
- Generates a Commit object with metadata
- Updates branch reference to point to new commit
- Branches are just files pointing to commit hashes
- Checkout updates HEAD and restores working directory
- Branch creation copies current commit reference