Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a file to my .gitignore without deleting it?

Tags:

git

gitignore

I have a configuration file in my project that is server specific so does not want to be included in Git as every time we deploy it overwrites the settings for the server. To resolve this problem the file was deleted from Git... nice idea but now every time the project is deployed the servers configuration file is deleted.

So I'm now stuck with a problem. What I really wanted was the file to be in .gitignore and never added to the project, but once it was added I can't see a way to remove it from the project without it firing off a delete on the existing file with every deploy.

Is there way I can remove a file from a project without deleting it. Effectively retrospectively ignoring a file that was already added?

like image 909
Horaland Avatar asked Oct 30 '22 10:10

Horaland


1 Answers

The easy and safe way to address this is to add the files you want to exclude to .gitignore.

If the file is already being tracked (previously added and committed) you will also need to delete it from the repository. This can be done without deleting the local file which you want to keep by telling git to only delete the tracked version.

git rm --cached <file> is the command that will do this. When you commit that change along with the .gitignore update, the repository will stop tracking and start ignoring that file.

like image 115
Brendon Whateley Avatar answered Nov 14 '22 02:11

Brendon Whateley