Automated Git Commits on Random Dates

Features

Prerequisites

Setup

1. Create a Private Repository

  1. Open GitHub and create a new private repository. 📂
  2. Clone the repository to your local machine using GitHub Desktop, VS Code, or the terminal. 🖥️

2. Initialize a Node.js Project

npm init -y

3. Install Required npm Packages

npm install moment jsonfile simple-git

4. Create Project Structure


project-directory/
├── data.json
├── index.js
├── package.json
        

5. Create data.json

{
  "date": "2023-12-06T00:00:00+05:30"
}

6. Create index.js


const FILE_PATH = './data.json';
const moment = require('moment');
const jsonfile = require('jsonfile');
const simpleGit = require('simple-git')();

const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;

const makeCommit = async (date, callback) => {
    const data = { date };
    jsonfile.writeFile(FILE_PATH, data, async () => {
        simpleGit.add([FILE_PATH]).commit(date, { '--date': date }, callback);
    });
};

const startDate = moment('2024-01-01');
const endDate = moment('2024-04-01');

const commitDates = [];
for (let date = startDate; date.isBefore(endDate) || date.isSame(endDate); date.add(1, 'days')) {
    const numCommits = randomInt(1, 5); // Random number of commits between 1 and 5
    for (let i = 0; i < numCommits; i++) {
        commitDates.push(date.clone().add(randomInt(0, 23), 'hours').format()); // Random hour of the day
    }
}

const makeCommits = async (dates) => {
    if (dates.length === 0) return simpleGit.push();
    const date = dates.shift();
    await makeCommit(date, () => makeCommits(dates));
};

makeCommits(commitDates);
        

Running the Script

  1. Open your terminal and navigate to your project directory. 💻
  2. Run the script using Node.js:
    node index.js
  3. The script will create random commits on random days within the specified date range and push them to your Git repository. 🚀

Notes

Example Output

After running the script, your Git history will include commits with random timestamps between the specified dates. 📜

Troubleshooting

This script simplifies creating artificial commit histories, ideal for learning, testing, or visualization purposes. 🌟