107

I have a file (config.php), that is already committed to a Git repository, but I want to ignore locally, i.e. I want that file to remain in repository, but force Git to ignore any changes to it.

I put the file into .gitignore, but it is still marked as changed and Git still is attempting to commit changes to it, every time I commit something.

Any ideas, what am I missing or doing wrong?

Dale K
  • 25,246
  • 15
  • 42
  • 71
prcaen
  • 2,429
  • 4
  • 25
  • 32
  • please add your .gitignore-file - that way we might see any problems regarding it. – Lars Aug 29 '11 at 15:40
  • possible duplicate of [How to ignore a file which is already comitted in the previous commit?](http://stackoverflow.com/questions/625919/how-to-ignore-a-file-which-is-already-comitted-in-the-previous-commit) – Karl Bielefeldt Aug 29 '11 at 18:58

3 Answers3

208

If the file is still displayed in the status, even though it is in the .gitignore, make sure it isn't already tracked.

git rm --cached config.php

If you just want to ignore it locally, you could also make it ignored by the git status:

git update-index --assume-unchanged config.php

As commented, do note that using --assume-unchanged might cause unwanted data loss as git stash resets the "ignored" files to the state in upstream, undoing local changes, without a warning or saving.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    OK thanks ! If I merge my branch with another, can this file makes some problems ? – prcaen Aug 29 '11 at 16:38
  • 1
    @Nimbus147: it shouldn't be an issue, and any changes on that file will still be ignored (ie not added/committed). – VonC Aug 29 '11 at 16:57
  • if use SmartGit, you should use remove to do this. – Omid.Hanjani Jan 06 '15 at 07:50
  • `git rm --cached config.php` will error out: `fatal: pathspec 'config.php' did not match any files`. What to do? – Sandra K Jul 14 '18 at 17:45
  • @San Do you have a config.phone file displayed by git status? – VonC Jul 14 '18 at 17:54
  • Can you help me in [my situation](https://stackoverflow.com/questions/51349901/how-to-track-untracked-files-in-git)? I would appreciate it really – Sandra K Jul 15 '18 at 15:51
  • 2
    Is there a way to force `update-index --assume-unchanged` on the remote branch? I'd like to keep the file in the repo, but not be able to be changed by others but moreover not available to stage. – bmikolaj Feb 08 '19 at 01:57
  • 1
    @p014k Not that I know of. For that kind of file, I keep it versioned under a different name, then use a content filer driver to automatically generate the right file (private) when needed. That filter would not be active on stage for instance. See https://stackoverflow.com/a/54454356/6309 and its associated link. – VonC Feb 08 '19 at 07:35
  • So it sounds like one can't ignore changes to a file that is part of the repo. – Nick May 06 '20 at 22:05
  • @Nick Indeed. You would need content filter driver to simulate partially ignoring a file content: https://stackoverflow.com/a/59484661/6309 – VonC May 07 '20 at 05:08
  • Using --assume-unchanged might cause unwanted data loss as `git stash` resets the "ignored" files to the state in upstream, undoing local changes, without a warning or saving. – user3351650 Dec 09 '20 at 09:16
  • @user3351650 Thank you, very good point. I have included your comment in the answer for more visibility. – VonC Dec 09 '20 at 09:21
  • This tried to delete my file. I don't want it deleted, I want it ignored. – Gringo Suave Apr 08 '22 at 18:41
  • @GringoSuave Do you mean "delete from the disk"? Because a `git rm --cached` should *keep* the file on your disk, not delete it. Only untrack it from Git, which is needed if you want the .gitignore to have any effect on it. – VonC Apr 08 '22 at 21:01
  • Yes, git status said it would remove file from repo, not delete the file. If the feature is not available, not your fault, however. – Gringo Suave Apr 08 '22 at 23:27
  • @GringoSuave Removing it from the repository is the expected outcome, in order to make the `.gitignore` effective, and actually ignoring the file (which is still on the disk, but no longer tracked in the repository) – VonC Apr 09 '22 at 00:05
  • 1
    From the question, "I want that file to remain in repository, but force Git to ignore any changes to it." Removing it is *not* the expected outcome. Personally need for everyone to ignore it, not just me, so the second command doesn't help either. – Gringo Suave Apr 09 '22 at 22:58
  • @GringoSuave Interesting: the file should be part of the repository history, but should never change? I usually remove it from the repo, keep a template file in the repo, with placeholder values in it, and use a content filter driver to generate a private (Ie: "non-tracked") file automatically on git checkout/restore. Example: the last section of my previous answer: https://stackoverflow.com/a/71391591/6309 – VonC Apr 09 '22 at 23:09
9

Ignore checked in file:

git update-index --assume-unchanged file

To revert

git update-index --no-assume-unchanged file

Revert All

git update-index --really-refresh 
shiva kumar
  • 11,294
  • 4
  • 23
  • 28
4

If the file is already in the repository, and hence the Index/Staging area, then an update to .gitignore won't change that situation. It would keep being committed.

To remove the file from the Index/Staging area use git rm <file>.

Thunder Rabbit
  • 5,405
  • 8
  • 44
  • 82
Philip Oakley
  • 13,333
  • 9
  • 48
  • 71
  • What if file is in the repository but is not staged yet? I am trying to ignore *.log files, but I still see them under "Changes". I want to ignore all, what to do? – Sandra K Jul 14 '18 at 17:46
  • @SandraK if there is an older version of that file (filepath/name) already in the repo at the last commit, then it is considered tracked - the 'index' will have that file in its records, and so `status` will continue to tell you about those changes. If you want to ignore *.log files, for both previously tacked and currently untracked fles, then you need to both `git rm *.log` and update your .gitignore file. Do check first if some log files are actually important... It is easy to forget that special one ;-) – Philip Oakley Jul 15 '18 at 16:51