1

I'd like to keep JavaScript (.js) files under git control.

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

Is there a way to achieve this with git?

EDIT: Preferably without changing directories and refactoring all the existing code.

chriskelly
  • 7,526
  • 3
  • 32
  • 50
  • 2
    I haven't used typescript but can't you ask typescript to generate the javascript files in a different directory? – redneb Sep 14 '16 at 23:18
  • 1
    Either append some identifier in the file, e.g., `_somefile.js` or output the JS to a different directory, e.g., `generated/somefile.js` – VLAZ Sep 14 '16 at 23:19
  • @redneb,@vlaz. It's not my call to move the files. I'd at least like my git repo to be tidy. Therefore, I was wondering if it can be done without having to move existing files and refactor all existing paths (not that I don't want to myself) – chriskelly Sep 15 '16 at 10:36

1 Answers1

5

A typical way to do this is to put any compiled output in some distribution directory. You might organized your directory structure like so:

src/
  ... various JavaScript files, etc.

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

Then in git, you can just ignore the dist directory, and assume that anyone who is going to pull down that repo and work on it will either run the build tool manually, or it will built automatically as part of the installation process.

In other words, it's easier to organize it without depending on a specific set of git functionality. This is a pretty common pattern in the JavaScript world.

If you don't go for that method, another way to do it is to add some identifier to the files that you want to ignore. For example, you might prepend all your files you want to ignore with _ and then add this rule to your .gitignore (see this answer for why the following works):

\_*

There are some drawbacks to this second approach:

  1. Other libraries you use may use filenames that start with a similar identifier and there will be collisions
  2. It's hard to maintain; you may want to change the identifier later if there is some collision or you change your mind about your structure, and you'll have to go back through all the files and change the names (of course, this can be automated, but it might be a pain)

Hope that helps.

Community
  • 1
  • 1
Josh Beam
  • 19,292
  • 3
  • 45
  • 68
  • 2
    `This is a pretty common pattern in the JavaScript world.` not only in JS. Pretty much anything that produces generated output will do this. In general the source/produced distinction is almost universal. It would be a build step to take all generated files and bundle them (somehow) into a deliverable, which, in this case, might be expressed as taking all `*.js` files and putting them in a `/js/` directory or something but excluding `*.ts` as that's not used the final product. – VLAZ Sep 14 '16 at 23:35
  • @JoshBeam: I've upvoted because it's a good suggestion. However, it doesn't answer my question. I am not permitted to move existing files and refactor. Therefore,for I'd like to find another way eliminate those files for consideration by git . – chriskelly Sep 15 '16 at 10:41