If you’ve added package-lock.json
to your .gitignore
file but Git is still tracking it, you’re not alone. This happens because .gitignore
only prevents new, untracked files from being added. If package-lock.json
was already committed earlier, Git will continue tracking it regardless of .gitignore
.
Here’s how to fix it:
Step 1: Remove package-lock.json
from Git’s index
Run the following command to stop tracking it, while keeping the file locally:
git rm--cached package-lock.json
Step 2: Commit the change
git commit -m "Stop tracking package-lock.json"
Step 3: Verify .gitignore
Make sure your .gitignore
file has this entry:
package-lock.json
Now Git will stop tracking changes to package-lock.json
.
⚠️ Important Note
Before doing this in a team project, discuss with your team. Many projects intentionally commit package-lock.json
to ensure consistent dependency versions across all environments. Ignoring it might lead to differences in installations.
If you really don’t want Git to track it, ensure everyone on your team applies the same change.
Optional: Ignore package-lock.json
Globally
If you don’t want Git to ever track package-lock.json
in any project on your machine, you can add it to your global .gitignore
:
1. Create or edit your global .gitignore
file:
git config --global core.excludesfile ~/.gitignore_global
2. Add this line to ~/.gitignore_global
:
package-lock.json
Now Git will ignore package-lock.json
in all repositories on your system.
✅ You’ve now learned how to ignore package-lock.json
locally in a project and globally across your system!