A custom implementation of Git's core functionality from scratch using Python. Learn how version control actually works under the hood.
This project replicates Git's essential features by building a version control system from the ground up. Instead of using Git's C implementation, this is a pure Python implementation designed to teach and demonstrate how modern VCS (Version Control Systems) work internally.
What you'll learn:
- How commits, branches, and merge algorithms work
- Object storage and compression in version control
- DAG (Directed Acyclic Graph) structures
- Content-addressable storage using hashing
- Merge conflict resolution
- ✅ Repository initialization — Create local
.mygitdirectories - ✅ Commit system — Store snapshots with SHA-1 hashing
- ✅ Branching — Create, switch, and list branches
- ✅ Merge operations — Fast-forward and recursive merges
- ✅ Status tracking — Monitor staged and unstaged changes
- ✅ Diff visualization — Compare versions
- ✅ Revert functionality — Undo commits safely
git clone https://github.com/devberatzengin/My_Own_Git.git
cd My_Own_Git
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt# Initialize a repository
python mygit.py init
# Stage changes
python mygit.py add <file>
# Create a commit
python mygit.py commit -m "Your message"
# Create a branch
python mygit.py branch <branch-name>
# Switch branches
python mygit.py checkout <branch-name>
# Merge branches
python mygit.py merge <branch-name>
# View status
python mygit.py status
# View history
python mygit.py logMy_Own_Git/
├── mygit.py # Main entry point
├── core/
│ ├── objects.py # Commit, Tree, Blob classes
│ ├── repository.py # Repository management
│ ├── staging.py # Index/staging area
│ └── merge.py # Merge algorithms
├── cli/
│ └── commands.py # CLI interface
├── utils/
│ ├── hash.py # SHA-1 hashing
│ └── diff.py # File diffing
├── tests/
│ ├── test_commits.py
│ ├── test_merge.py
│ └── test_branches.py
└── requirements.txt
Object Model:
- Blob — File content (immutable)
- Tree — Directory snapshot
- Commit — Metadata + parent reference + tree
- Ref — Pointer to commit (branch/tag)
Storage:
.mygit/
├── objects/ # Content-addressable storage
│ ├── <hash[0:2]>/
│ │ └── <hash[2:]> # Zlib-compressed object
├── refs/
│ ├── heads/ # Branch pointers
│ └── tags/ # Tag pointers
└── HEAD # Current branch reference
Merge Algorithm:
- Three-way merge for automatic resolution
- Conflict markers for manual cases
- Recursive history traversal using LCA (Lowest Common Ancestor)
User creates file → Add to staging → Commit
↓
Create blob + tree
↓
Hash objects + store in .mygit/objects
↓
Update branch ref to new commit SHA
↓
Update HEAD if on that branch
# Run all tests
python -m pytest tests/ -v
# Run specific test
python -m pytest tests/test_commits.py -v
# With coverage
pytest --cov=core tests/After studying this project, you'll understand:
- How Git stores data — Content-addressable storage model
- Commit structure — Snapshots vs. deltas
- Branch mechanics — Just pointers to commits
- Merge algorithms — Three-way merge + conflict detection
- Performance — Why Git is fast (DAGs, compression)
- Remote repositories (push/pull simulation)
- Rebase functionality
- Cherry-pick operations
- Stash management
- Hook system (pre-commit, post-commit)
- Performance optimization with C extensions
Found a bug or want to improve the merge algorithm? Pull requests are welcome!
- Fork the repository
- Create your feature branch (
git checkout -b feature/improvement) - Commit your changes (
git commit -m 'Add improvement') - Push to the branch (
git push origin feature/improvement) - Open a Pull Request
Open an issue or reach out on LinkedIn