Git ignore generated extensions

I want to store JavaScript files (.js) under a git control.

However, if a TypeScript (.ts) file exists, I would like to ignore the associated .js file.

Is there a way to achieve this with git?

EDIT : preferably without changing directories and refactoring all existing code.

+1


source to share


1 answer


The typical way to do this is to put any compiled output in some distribution directory. You could organize your directory structure like this:

src/
  ... various JavaScript files, etc.

dist/
  ... compiled output from any build tool you're using

      

Then in git, you can just ignore the directory dist

and assume that anyone who is going to pull that repo and work on it will either run the build tool manually or be automatically built as part of the installation process.

In other words, it is easier to organize it regardless of the specific git feature set. This is a fairly common pattern in the JavaScript world.

If you don't go for this method, another way to do it is to add some identifier to the files you want to ignore. For example, you can add all your files you want to ignore with _

, and then add that rule to yours .gitignore

(see this answer why the following works):



\_*

      

There are some disadvantages to this second approach:

  • Other libraries used may use filenames starting with the same identifier and collisions will occur.
  • Difficult to maintain; you may want to change the id later if there is some kind of collision, or you change your mind about your structure and you have to go back through all files and change names (of course this can be automated, but it can be a pain)

Hope it helps.

+5


source







All Articles