- Introduction
- Features
- Prerequisites
- Installation
- Usage
- Example Workflow
- Project Structure
- Understanding the Code
- Limitations
- Future Enhancements
- Contributing
- License
MiniGit is a simplified version of Git implemented in Python. It demonstrates the core concepts of version control systems by allowing you to initialize a repository, add files, and commit changes. This project is intended for educational purposes to help understand how Git works under the hood.
- Initialize a Repository: Create a new MiniGit repository.
- Add Files: Add files to the staging area (index).
- Commit Changes: Commit staged changes with a message.
- Object Storage: Store file contents and commits as hashed objects.
- Python 3.x: Ensure you have Python 3 installed on your system.
- Basic Knowledge of Git: Familiarity with Git concepts will help in understanding MiniGit.
-
Clone the Repository (Optional):
If you're accessing this code from a repository, you can clone it:
git clone https://github.com/MayankShrivastava17/minigit.git cd minigit
To start using MiniGit, you need to initialize a repository. This creates a .minigit
directory in your current working directory, which will store all MiniGit data.
Example:
from minigit import MiniGit
# Initialize the MiniGit repository
repo = MiniGit()
repo.init()
Output:
Initialized empty MiniGit repository in .minigit
Adding files stages them for the next commit. MiniGit will hash the content of the file and store it in the .minigit/objects
directory.
Example:
First, create a sample file to add:
echo "Hello, MiniGit!" > test.txt
Now, add the file using MiniGit:
repo.add("test.txt")
Output:
Added file test.txt
Commit the staged changes with a descriptive message. This creates a commit object that captures the state of the index at that point in time.
Example:
repo.commit("Initial commit")
Output:
Committed with message: Initial commit
Commit hash: a1b2c3d4e5f6...
Below is a step-by-step example of using MiniGit to track changes in a project.
1. Initialize the Repository
from minigit import MiniGit
repo = MiniGit()
repo.init()
2. Create and Add a File
Create a new file hello.txt
:
echo "Hello, World!" > hello.txt
Add the file to the staging area:
repo.add("hello.txt")
3. Commit the Changes
repo.commit("Add hello.txt")
4. Modify the File and Commit Again
Make changes to hello.txt
:
echo "This is an update." >> hello.txt
Add and commit the changes:
repo.add("hello.txt")
repo.commit("Update hello.txt")
5. Check the .minigit/objects
Directory
List the contents of the objects directory:
ls .minigit/objects
You will see hashed files representing the stored objects.
your_project/
├── minigit.py
├── test.txt
├── hello.txt
└── .minigit/
├── index
└── objects/
├── <file_hashes>
└── <commit_hashes>
minigit.py
: The MiniGit class implementation.test.txt
,hello.txt
: Sample files you're tracking..minigit/
: Directory storing MiniGit data.index
: Tracks the staging area.objects/
: Stores hashed file contents and commits.
Here's a breakdown of how MiniGit works internally.
class MiniGit:
def __init__(self, repo_dir=".minigit"):
# Initialization code
- Initialization: Sets up the repository directory and creates necessary subdirectories and files.
def _hash_object(self, data):
return hashlib.sha1(data.encode()).hexdigest()
- Purpose: Generates a SHA-1 hash of the given data, similar to how Git hashes objects.
def _write_index(self, index_data):
# Writes the index to a file
def _read_index(self):
# Reads the index from a file
- Index File: Acts as the staging area, tracking files added before committing.
def add(self, file_path):
# Adds a file to the index
- Process:
- Reads the file content.
- Hashes the content and stores it in
objects/
. - Updates the index with the file path and its hash.
def commit(self, message):
# Commits the current state of the index
- Process:
- Reads the current index.
- Creates a commit object containing the index and the commit message.
- Hashes and stores the commit object in
objects/
.
- No Branching: Does not support branches or merging.
- No Diffs: Cannot show differences between commits.
- No File Removal: Does not track file deletions.
- No Remote Repositories: Cannot push or pull from remote locations.
- Limited Error Handling: Basic validation is implemented.
- Branch Support: Implement branching and merging capabilities.
- Diff Tool: Add functionality to compare different commits.
- File Deletion Tracking: Allow tracking of removed files.
- Remote Operations: Enable pushing to and pulling from remote repositories.
- User Interface: Develop a command-line interface (CLI) for easier interaction.
Contributions are welcome! If you'd like to contribute to MiniGit, please follow these steps:
- Fork the Repository
- Create a Feature Branch
git checkout -b feature/YourFeature
- Commit Your Changes
git commit -m "Add YourFeature"
- Push to Your Fork
git push origin feature/YourFeature
- Open a Pull Request
This project is licensed under the MIT License. See the LICENSE file for details.
Note: MiniGit is a simplified educational tool and is not intended to replace Git for production use.