1

My .gitignore contains dist folder.

I have a file: dist/monorepo-docker-build-helper.js which I need to track from now on (it's auto generated from a build-process).

.gitignore file now:

dist
!dist/monorepo-docker-build-helper.js

When running git status, dist/monorepo-docker-build-helper.js is never listed, even after it was changed.

I looked at other answers: git add dist/monorepo-docker-build-helper.js -f helps but when there is a new change, git status doesn't show it.

What can I do to permanently add dist/monorepo-docker-build-helper.js to git?

Stav Alfi
  • 13,139
  • 23
  • 99
  • 171
  • Did `git add dist/monorepo-docker-build-helper.js` (without the `-f`-option) also work or did you need it? In case you need it, it seems the file is still getting ignored. – SimplyComple0x78 Jan 30 '21 at 15:43

2 Answers2

1

In your case, you need to ignore not the dist folder itself, but everything in it (dist/*). This way the exclude afterwards will work as stated in this post.

You should really rethink why you want to commit generated files in the first place, because it's a bad practice to do so.

  • it's working. But I have multiple `dist` folders inside the project. after trying your solution, git tries to follow all `dist` folders. – Stav Alfi Jan 30 '21 at 15:58
  • by adding third line to `.gitignore`: `packages/**/dist` it seems that now only one file from one `dist` folder is tracked by git. – Stav Alfi Jan 30 '21 at 16:01
  • Huh, strange enough. Seems that the [pattern format](https://git-scm.com/docs/gitignore#_pattern_format) interprets the lines relative as soon as any slash is in there. The both lines `/dist/*` combined with `/!dist/myfile.js` (or `!/dist/myfile.js`?) (note the **prefixed slash** should at least be sufficient, can you confirm that? – SimplyComple0x78 Jan 30 '21 at 17:58
1

I believe in this case you have to exclude dist/* in the first rule, otherwise for Git the dist folder does not exist.

dippas
  • 58,591
  • 15
  • 114
  • 126
Pedro
  • 169
  • 1
  • 8