using gitignore to ignore (but not delete) files

Solution 1:

Instead of .gitignore, you can update local git repository by running following command:

git update-index --assume-unchanged <file>

In this case a file is being tracked in the origin repo. You can modify it in your local repo and git will never mark it as changed. Read more at:

  • http://blog.pagebakers.nl/2009/01/29/git-ignoring-changes-in-tracked-files/ - was reported dead at some time (sorry, not mine)
  • http://archive.robwilkerson.org/2010/03/02/git-tip-ignore-changes-to-tracked-files/ - another one covering the same topic

Solution 2:

Ignoring changes made to files while allowing them to exist is the exact purpose of .gitignore. So adding the files (or directories) to .gitignore is the only thing you have to do.

But your problem is that git is already tracking the files you want to ignore and .gitignore doesn't apply to tracked files. The only way to stop this tracking is to tell git to remove them. By using git rm --cached, you prevent git from deleting your local files, but any other repository getting your changes will apply the removal. I don't think there's a way to avoid that from your own repository. You must do something on the other repositories, or accept the files will be removed.

To prevent the removal on each other repository you can:

  • (obviously) backup the files somewhere, pull the changes and restore the files,
  • or also git rm --cached the files and commit before pulling your changes. Git will nicely merge the two removals without touching the already untracked files.

Solution 3:

Put a / at the end of the directory name in your .gitignore file, i.e.

tmp/

If have tracked files inside that directory, you need to tell git to forget about them first (before you add the dir to the ignore list). Assuming you have nothing vital in there (i.e. that you can scratch it):

git rm -rf ./tmp/
git commit -m "untrack tmp dir"
mkdir tmp
echo tmp/ >> .gitignore
git add .gitignore ; git commit -m "add tmp/ to ignore list"

New files in that directory will not be tracked.

The --cached option to git rm only works on the index (pending changes more or less). It has no effect on the working tree or the status of what has been tracked or not.

Solution 4:

.gitignore has no effect on tracked files.

What you want is to set the assume-unchanged bit on the files in the tmp/ directory. There's a good explanation how to do that here: Git: untrack a file in local repo only and keep it in the remote repo

Also, one-liners for setting assume-unchanged on all files in a directory - git update-index --assume-unchanged on directory .