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 initCreates 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 filesStages 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 branchnamecheckout -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 logDisplays up to 10 commits from current branch:
commit hash
author
- Repository Status
python main.py statusShows:
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.