Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git ignore changes to committed file

Tags:

git

gitignore

I have a config file in my repo that shouldn't get changes committed to it.

The problem is that, when I make changes to the file they are picked up by git status. I've tried various ways to ignore changes to it.

How can it be automatically ignored when I clone the repo, so that any changes I make to the file shouldn't be picked up by git?

Solutions I've tried:

  1. Adding the file to .gitignore. Doesn't seem to work. Is this because the file is already on the index?

  2. Using git update-index --assume-unchanged path/to/file. Seemed to work some what but is only local to my cloned repo. Others who clone need to apply the same command.

I've tried looking at other answers to this but other solutions don't seem to work properly. I feel like this should be quite simple, so any guidance would be helpful!

like image 236
David Normington Avatar asked Nov 10 '22 20:11

David Normington


1 Answers

I have a config file in my repo that shouldn't get changes committed to it.

Then... don't commit that file in the first place.
Commit a generic version of that file, a template used to generate the actual file (as a private non-versioned file, added to your .gitignore).

That generation would be automatic on git checkout.

First, rename your existing file:

git mv afileToIgnore afile.tpl
git commit -m "record template value file"

Then add to afileToIgnore to a .gitignore file: a git status won't show it anymore to be added/changed.

Now, declare a smudge content filter driver which, automatically, will re-generate that file (ignored, since it is in .gitignore)

https://git-scm.com/book/en/v2/images/smudge.png

smudge script (which you can version): YourScript

copy aFileToIgnore.tpl aFileToIgnore

Declare the content filter driver in a versioned .gitattributes:

echo 'aFileToIgnore.tpl config' >> .gitattributes

That smudge 'config' content filter driver needs to be activated locally by each user cloning that repo.

cd /path/to/repo
git config filter.config.smudge YourScript

That last step is the only one each user need to do in order to benefit from the actual config file to be automatically generated on each git checkout.


Others who clone need to apply the same command.

True, but here, if they don't, they won't have the config file at all.
That is more visible than having the file ready to be modified when it should not be.

like image 101
VonC Avatar answered Nov 15 '22 06:11

VonC