DEV Community

CodeNameGrant
CodeNameGrant

Posted on

Stop Git from tracking file changes

In this short post I'll explain how and why I like to (sometimes) stop Git from tracking file changes.

Why would I want such a thing?

A simple but real world use case would be when your LOCAL project config differs from the config required in PROD.

Take a look at this .npmrc file. Its configured to make use of use an environment variable ${NPM_GIT_PAT} as the authToken when connecting to the GitHub Package registry.

@my-private-scope:registry=https://npm.pkg.github.com/
//npm.pkg.github.com/:_authToken=${NPM_GIT_PAT}
Enter fullscreen mode Exit fullscreen mode

Now the file in this state is ready for PROD. When it runs (via GitHub Actions) the NPM_GIT_PAT environment variable will be declared and populated by a GitHub secret and NPM will be able to install dependencies from a private repo.

But what about your LOCAL environment? You need to set that authToken to your own private PAT so you can access those private repos. By telling git to stop tracking changes on the npmrc file, you can edit it and paste your private token in there without the risk of later accidentally committing it your repo.

How

The how is the easy part, git provides a command that let you tell it to stop tracking specific files or folders.

git update-index --assume-unchanged path/to/src/file
Enter fullscreen mode Exit fullscreen mode

This should only be used on files that are already in your repo, if you want git to ignore files from the get-go, add them to you .gitignore file instead.

"Wait, how do I undo it?"
Its just as easy, replace --assume-unchanged with --no-assumed-unchanged and you're set.

git update-index --no-assume-unchanged path/to/src/file
Enter fullscreen mode Exit fullscreen mode

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay