git logo

How to clean up redundant .js, .jsx and .d.ts files from your repository?

This post was most recently updated on July 31st, 2023.

2 min read.

Even if you have a 100% TypeScript project, it’ll still end up with a lot of .js (JavaScript) or .jsx (JavaScript with React extension) files. Unless you properly ignore them, they’ll end up cluttering your Visual Studio solution, and even git.

As these are, in that case, generated files and essentially build artifacts, you will most likely not want to commit them to your code repository. They’ll get recreated during the build anyway and are more likely to cause confusion than to do any good.

So you should not care about them or have them in version control.

But what if you’re inheriting someone else’s solution, they already committed every single generated .js, .jsx and even the type declaration (.d.ts) files. Thousands of unnecessary files cluttering the repository. How do you gracefully nuke them out of existence?

Well, that’s just what I’m going to write about today! So buckle up, start your terminal and prepare for a very short flight!

Solution

We’ll need to make sure the files are ignored by git, and ensure they are also removed from the remote repository. And here’s how:

Time needed: 5 minutes

How to remove unnecessary .js, .jsx and .d.ts files from a git repository?

  1. Update your .gitignore file

    First we’ll update your .gitignore file to instruct git to ignore these files in the future.

    Find (or create) your .gitignore file at the root of your repository, and make sure it has these lines:

    #┏┓
    #┃┃╱╲ in
    #┃╱╱╲╲ this
    #╱╱╭╮╲╲house
    #▔▏┗┛▕▔ we
    #╱▔▔▔▔▔▔▔▔▔▔╲
    # Ignore generated files (we use TypeScript)
    #╱╱┏┳┓╭╮┏┳┓ ╲╲
    #▔▏┗┻┛┃┃┗┻┛▕▔
    *.js
    *.jsx
    *.d.ts


    (Feel free to ignore the ASCII art if you want to be a buzzkill)

    After this, no further files will be committed. But how do we remove the existing ones from the version control?

  2. Remove the files from your local repository (temporarily)

    Alright, now we’ll want to remove the files from the repository. The easiest way to do that is to first remove them locally and then commit the changes. The files will be generated on build so nothing of value will be lost.

    I know what your instincts say, but don’t just Clean Solution. I don’t think it is trustworthy to clear generated files (while it does clean build artifacts). Run a simple script for the removal instead. This will look somewhat like this:

    cd yourdir
    DEL /S /Q *.js *.jsx *.d.ts


    DEL does what it sounds like, but /Q will make it not ask for each file and /S means something like “subdirectories”, so it’ll be recursive. I know, I know – /S sounds like “silent” and /Q looks like “query” or something, but we can’t always have the logical switches we want, can we?

  3. Commit the changes

    Now that you’ve removed the files from your disk and made sure they should be ignored in the future, you can commit the update to .gitignore and all the removals of the files.

And with that, you might’ve just made someone else’s life just a little bit more simple when they inherit this project after you! And of course there are a few more files to compare differences to, so maybe your git commands are going to be ever-so-slightly-faster, too. Justifies using a few minutes of scripting, right? :)

mm
5 2 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments