npm init -y
npm install moment jsonfile simple-git
project-directory/
├── data.json
├── index.js
├── package.json
data.json{
"date": "2023-12-06T00:00:00+05:30"
}
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);
node index.js
startDate and endDate variables in index.js to customize the date range.After running the script, your Git history will include commits with random timestamps between the specified dates. 📜
data.json file path and structure if errors occur. 🛠️This script simplifies creating artificial commit histories, ideal for learning, testing, or visualization purposes. 🌟