How to ignore a tracked file in git without deleting it

gitignoresourcetree

My team uses sourcetree as our git client. There is a third-party plugin in our project. It needs several configuration files. These files cannot be generated automatically. They store account name, login tokens and some temporary options, which shouldn't be shared. But everyone still needs this file, otherwise it will report error. So now they always stay in our "uncommitted changes" section which is annoying.

I have 2 options:

  1. Remove these files from git repository. Add them into .gitignore. Ask all my team members to add these files back to their local project with their own settings.

  2. See if there are tricks like "ignoring a tracked file without deleting it".

(Basically I feel if option 2 was possible, git should have some logic like "If local doesn't have this file, use remote version. If local has this file, ignore". The logic feels bad, easy to generate bugs.)

Best Answer

This is what you want to do:

  1. Add all the files, individually or in a folder, that you want to remove from the repo but keep locally to .gitignore.
  2. Execute git rm --cached put/here/your/file.ext for each file or git rm --cached folder/\* if they are in a folder. (It is /\* because you need to escape the *)
  3. Commit your changes.
  4. Push to remote.

After having done this, you will effectively "ignore tracked files without deleting them", removing them from the repo and keeping all of them untouched in your system. Then when your team pulls the changes, they will pull the .gitignore file and their individual configuration files will not be overwritten by the ones in the repo because you have ignored them and deleted them from it. Also, because now the new .gitignore is ignoring these files, everybody will have their own version of them not showing in the "uncommitted changes".

Related Question