DEV Community


Posted on

How to change a file locally without commit it on Git


I wanted to freeze a file in git, so that when someone commits, this file is not updated.
The scenario was something like that: I need an ENV variable that usually will be available only on CI/CD to take the right token but on the local side I don't need it.

The Problem

So I want a file that will be on my repository, and will be versioned but I want to change it on the local side and not commit it (I can skip it every time without include it on git add but it's so boring and in long term will not work).
I tried a different hack solution like .gitignore but it did not work.
So I read some documentation and I discovered the update-index option on git.

How update-index works

With this command:
git update-index --assume-unchanged <file>
Are you basically changing the assume unchanged bit and are you forcing Git to pretend it never changes.
In this way Git will exclude this file from any tracking, so you can modify it on the local side and basically forget it.

If you want to came back to the "normal version" u can easily exec this command:
git update-index --no-assume-unchanged <file>
and everything will come back like before.

If u want to check what kind of files are with this active flag you can easily run:
git ls-files -v
Files where the flag is --assume-changed have H, --assume-unchanged is h.

Top comments (0)