Skip to content

MiniGit is a simplified version of the Git version control system, implemented in Python for educational purposes. It provides a basic understanding of how Git works behind the scenes by offering core functionalities like initializing a repository, staging files, and committing changes.

License

Notifications You must be signed in to change notification settings

MayankShrivastava17/MiniGit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

MiniGit: A Simplified Git Implementation in Python

Table of Contents


Introduction

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.

Features

  • 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.

Prerequisites

  • 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.

Installation

  1. 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

Usage

1. Initialize a Repository (git init)

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

2. Add Files to the Index (git add)

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

3. Commit Changes (git commit)

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...

Example Workflow

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.

Project Structure

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.

Understanding the Code

Here's a breakdown of how MiniGit works internally.

The MiniGit Class

class MiniGit:
    def __init__(self, repo_dir=".minigit"):
        # Initialization code
  • Initialization: Sets up the repository directory and creates necessary subdirectories and files.

Hashing Objects

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.

Index Management

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.

Adding Files

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.

Committing Changes

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/.

Limitations

  • 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.

Future Enhancements

  • 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.

Contributing

Contributions are welcome! If you'd like to contribute to MiniGit, please follow these steps:

  1. Fork the Repository
  2. Create a Feature Branch
    git checkout -b feature/YourFeature
  3. Commit Your Changes
    git commit -m "Add YourFeature"
  4. Push to Your Fork
    git push origin feature/YourFeature
  5. Open a Pull Request

License

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.

About

MiniGit is a simplified version of the Git version control system, implemented in Python for educational purposes. It provides a basic understanding of how Git works behind the scenes by offering core functionalities like initializing a repository, staging files, and committing changes.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages