40

With npm v5 here is now package-lock.json file being created after npm install

It is recommended to commit this file, but I have issue that this file is different for some reason between my dev machine and my server. Even if I push that file to repo, after npm install on server, file changes.

So now I'm attempting to make git untrack this file. After following numerous answers from other questions, I seem to have almost managed to do so, it's not tracked on dev machine, doesn't appear in the repo itself, but after I pull code to server and make npm install, it appears in modified files.

File is in .gitignore, but server git for some reason ignores it.

git check-ignore -v -n package-lock.json
::      package-lock.json
git check-ignore -v -n --no-index package-lock.json
.gitignore:10:package-lock.json package-lock.json 

Possibly relevant info:

Dev machine: Windows 10. Server: Ubuntu 14.04. I'm pulling code to server using tags.

Giedrius
  • 1,590
  • 3
  • 16
  • 27
  • 2
    Are you sure you have tried to use `git rm [FILENAME] -f` and then add the file to your `.gitignore` like `/[FILE_PATH_RELATIVE_TO_REPO]`? After you commit, it should no longer appear. – ifconfig Jun 17 '17 at 04:29
  • *"this file is different for some reason between my dev machine and my server"* -- `package-lock.json` is updated by `npm update` and by the `npm` commands that modify `package.json`. You are supposed to run these commands only on the development machine. The only command to run on the production server is `npm install`. – axiac Jul 19 '18 at 08:00

2 Answers2

95

You need to remove it from your repo (git rm package-lock.json) in order for git to stop tracking it.

.gitignore only works for untracked files. If you have a tracked file that is also in your .gitignore, the fact that the file is tracked overrides the fact that it is also in .gitignore.

Andy
  • 30,088
  • 6
  • 78
  • 89
  • Yep, that did. I have already done that previously, pushed changes and merged to master. But somehow I have managed to pull on server earlier version and I was under impression that it didn't work. Rechecked everything again now and spotted this error of mine, pulled newest version it voilà! Thanks. – Giedrius Jun 17 '17 at 04:38
  • great it works. if you have already any file in your repo and then you want git not to track it. you gotta remove it permanently first from repo. do that by above command and push to remote branches. next time if anyone creates this file it wont be tracked if it is in .gitignore. – Jaydeep Galani Apr 29 '20 at 08:31
6
  • to stop tracking it, add the file to .gitignore (you already did that)
  • to remove it from the repo without deleting the file, call git rm --cached package-lock.json
  • call git status to see the file is marked as deleted, and the file is not listed as untracked (because it is gitignored), but it still exists

commit your changes and push if needed.

commonpike
  • 10,499
  • 4
  • 65
  • 58