1

I want to push config.js once(like a default config) to repo. But when I push it, add to gitignore and when I try to pull, it removing my config.js. How I should do it right?

Andrey Kadnikov
  • 455
  • 5
  • 13

3 Answers3

2

After you've pushed the commit that contains config.js to the remote repo, you can tell Git to stop checking it for changes with:

git update-index --skip-worktree path/to/config.js

If you want to commit some changes to it later, you'll need to first tell Git to start checking it again with:

git update-index --no-skip-worktree path/to/config.js

Note that this only applies to your local repository―everyone who clones the repo will have to run the same command in order to stop tracking changes to the config.js file, which may or may not be a problem.

A common alternative solution is the approach described by @k0pernikus, that is to commit the default configuration in a separate file (e.g. config.js.default) and have a script that copies and renames that file to config.js if it doesn't already exist.

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
2

I'd propose another solution:

  • Add config.js to .gitignore (and completely delete it from the repo)
  • Add a file config.js.template as part of your repo with your default values
  • in your README (and maybe also a part of a setup script), make other users copy config.js.template to config.js and fill in their values

This way you don't mistakenly add changes to the config.js and you make its creation part of your bootstrapping process.

There are ways to tell git to treat a file as unchanged, yet I consider it not worth it after having wrestled with it.

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
0

A solution that goes more in hand to your attempted solution (though one I would not really recommend) would be to use:

git update-index --assume-unchanged <file>

after making changes to your local config.js.

If you want to undo it you can use:

git update-index --no-assume-unchanged <file>

Keep in mind that these changes do not propagate to the remotes. So for every clone you have to explicitly tell git to assume that a file did not change.

k0pernikus
  • 60,309
  • 67
  • 216
  • 347