A lightweight, educational reimplementation of Git in Python. This project demonstrates how Git works under the hood by managing commits, branches, trees, and blobs without relying on Git itself.
Clone the Repository
git clone https://github.com/vedantvisoliya/Python-Git-Clone.git
Run Commands
Use python or python3 prefix depending on your system:
python main.py <command>
- Initialize Repository
python main.py init
Creates a .pygit folder containing:
objects/ β Stores Git objects (blobs, commits, trees).
ref/heads/ β Stores branch references.
index β Staging area (dictionary of file paths β SHA-1).
HEAD β Stores current branch.
- Add Files
python main.py add hello.txt # files
python main.py add . # direcotry
python main.py add hi.txt index.py go.txt # list of files
Stages files in the index (staging area).
Uses relative paths as keys and SHA-1 hashes as values.
- Commit Changes
python main.py commit -m "message"
python main.py commit -m "message" --author "Name <email>"
Creates a tree object for the whole directory.
Stores commit with:
commit message
timestamp
author
parent hashes
After committing:
index is cleared (stagging area emptied).
Commit object saved in .pygit/objects/.
- Checkout Branch
python main.py checkout -b newbranch
python main.py checkout branchname
checkout -b: Creates a new branch under .pygit/ref/heads/branchname.
checkout branchname: Switches branch, rebuilds the directory from the tree hash.
- Branch Management
python main.py branch -b branchname # Create & switch
python main.py branch -d branchname # Delete & move to master
python main.py branch # List all branches
- View Commit Logs
python main.py log
Displays up to 10 commits from current branch:
commit hash
author
- Repository Status
python main.py status
Shows:
Changes to be committed
Unstaged files
Deleted files
Untracked files
GitObject Class β Base class for Git objects.
Blob β Stores file content.
Tree β Represents directory structure.
Commit β Stores commit metadata.
All objects are stored in .pygit/objects/ and referenced via SHA-1.
-
Garbage collector for unused objects.
-
Stash functionality.
-
Merge branches.
-
Cherry-pick commits.
-
Checkout a commit (detached HEAD).
-
Tag support.
Pull requests and feedback are welcome! Open an issue or suggest improvements.