• Blog
  • Documentation
  • Courses
  • Changelog
  • AI Starters
  • UI Kit
  • FAQ
  • Supamode
    New
  • Pricing

Launch your next SaaS in record time with Makerkit, a React SaaS Boilerplate for Next.js and Supabase.

Makerkit is a product of Makerkit Pte Ltd (registered in the Republic of Singapore)Company Registration No: 202407149CFor support or inquiries, please contact us

About
  • FAQ
  • Contact
  • Verify your Discord
  • Consultation
  • Open Source
  • Become an Affiliate
Product
  • Documentation
  • Blog
  • Changelog
  • UI Blocks
  • Figma UI Kit
  • AI SaaS Starters
License
  • Activate License
  • Upgrade License
  • Invite Member
Legal
  • Terms of License

Get changed files in a git repository with Node.js

Dec 26, 2022

This snippet will help you retrieve the list of the currently changed files in a git repository using Node.js.

It can be useful to retrieve the list of the currently changed files in a git repository.

For example, you might want to run a script only if a specific file has been changed, such as when indexing a search engine with new content that has been added to a repository: we use this technique in this website when a new post is added or an existing one is updated.

To write the script, we will use:

  1. the git diff command
  2. and Node.js
import { execSync } from 'child_process';
function getChangedFiles(extension: string = '') {
const extensionFilter = extension ? `-- '***.${extension}'` : '';
const command = `git diff HEAD^ HEAD --name-only ${extensionFilter}`;
const diffOutput = execSync().toString(command);
return diffOutput.toString().split('\n').filter(Boolean);
}

The getChangedFiles function takes an optional extension parameter that will filter out the files that do not have the given extension.

For example, if we want to retrieve the list of the changed files that have a .mdx extension, we can call the function like this:

const changedFiles = getChangedFiles('mdx');

The getChangedFiles function will return an array of the changed files whose path starts from the current directory.

That's it! Hope you found this snippet useful. Ciao!

Some other posts you might like...
Oct 22, 2025Next.js 16: what's new?Next.js 16 is a major release that includes several new features and improvements. In this article, we will cover the new features and improvements in Next.js 16.
Oct 19, 2025React 19.2: Upgrade GuideReact 19.2 introduces the Activity component, useEffectEvent hook, Partial Pre-rendering, and Chrome DevTools Performance Tracks. Learn about all the new features and improvements in this comprehensive guide.
Oct 13, 2025Build a Production Supabase Blog in Next.js with Supamode CMSLearn how to design a Supabase blog schema, build a Next.js UI, and wire up Supamode CMS so your team can publish fast without touching SQL
Sep 25, 2025Mastering AI-Driven Development: Claude Code & Makerkit Best PracticesA comprehensive guide to building production-ready SaaS features using Claude Code's intelligent agents and Makerkit's PRD functionality