How to ignore files which are in repository?

35,676

Solution 1

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.

Solution 2

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 

Solution 3

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>.

Share:
35,676

Related videos on Youtube

prcaen
Author by

prcaen

Pierrick CAEN, Android Developer pierrickcaen.fr | @prcaen

Updated on March 15, 2022

Comments

  • prcaen
    prcaen about 2 years

    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?

  • prcaen
    prcaen over 12 years
    OK thanks ! If I merge my branch with another, can this file makes some problems ?
  • VonC
    VonC over 12 years
    @Nimbus147: it shouldn't be an issue, and any changes on that file will still be ignored (ie not added/committed).
  • Omid-RH
    Omid-RH over 9 years
    if use SmartGit, you should use remove to do this.
  • Sandra K
    Sandra K almost 6 years
    git rm --cached config.php will error out: fatal: pathspec 'config.php' did not match any files. What to do?
  • Sandra K
    Sandra K almost 6 years
    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?
  • VonC
    VonC almost 6 years
    @San Do you have a config.phone file displayed by git status?
  • Sandra K
    Sandra K almost 6 years
    Can you help me in my situation? I would appreciate it really
  • Philip Oakley
    Philip Oakley almost 6 years
    @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 ;-)
  • p014k
    p014k about 5 years
    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.
  • VonC
    VonC about 5 years
    @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 stackoverflow.com/a/54454356/6309 and its associated link.
  • Adem Tepe
    Adem Tepe over 4 years
    Helpful. Revert one file worked but revert all didn't, though.
  • Nick
    Nick almost 4 years
    So it sounds like one can't ignore changes to a file that is part of the repo.
  • VonC
    VonC almost 4 years
    @Nick Indeed. You would need content filter driver to simulate partially ignoring a file content: stackoverflow.com/a/59484661/6309
  • user3351650
    user3351650 over 3 years
    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
    VonC over 3 years
    @user3351650 Thank you, very good point. I have included your comment in the answer for more visibility.
  • Gringo Suave
    Gringo Suave about 2 years
    This tried to delete my file. I don't want it deleted, I want it ignored.
  • VonC
    VonC about 2 years
    @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.
  • Gringo Suave
    Gringo Suave about 2 years
    Yes, git status said it would remove file from repo, not delete the file. If the feature is not available, not your fault, however.
  • VonC
    VonC about 2 years
    @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)
  • Gringo Suave
    Gringo Suave about 2 years
    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.
  • VonC
    VonC about 2 years
    @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: stackoverflow.com/a/71391591/6309