Search code examples
gitadditiongitignoreignore

Keep existing file in repo while being added to .gitignore


Currently, we have some project property files up on the repo that I'm willing to add to .gitignore so that they get ignored from now on even if we change them locally.

I've already tried:

git rm <file> --cached

Add that file to .gitignore and then a git add -f <file> so that it gets uploaded to the repository but that will just blow up gitignore and upcoming changes to that file will get tracked by git, which is the thing that I'm trying to avoid.

So, to sum up:

-A file is currently up on the repo. -I want to keep it on the repo. -That file must NOT be changed from now on while still being up on the repo so I'll add it on gitignore. -Git must not track changes on that file.

Thank you by advance.


Solution

  • If I understand you correctly, you can achieve this behavior with git update-index --assume-unchanged <file>.

    And you can undo the setting with git update-index --no-assume-unchanged <file>.

    This will ignore all future modifications (locally) of that file, while the file itself is still in the git repository. Note that this effect is only local. So if another person clones your repository, this file will behave as usual.

    Further Information: https://git-scm.com/docs/git-update-index#git-update-index---no-assume-unchanged