This post was most recently updated on July 31st, 2023.2 min read.
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!
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?
- 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:
# Ignore generated files (we use TypeScript)
(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?
- 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:
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?
- 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? :)
- How to install Home Assistant on Synology NAS (DSM 7.2)? - August 29, 2023
- How to configure a custom 404 page for a SharePoint site? - August 22, 2023
- Azure DevOps pipelines suddenly failing with “##[error]#0 building with default instance using docker driver” - August 15, 2023