ignoring files using GIT

Just a small writeup of ignoring files in GIT. Every developer uses GIT or another code versioning system. Right?

Sensitive settings do not belong in Git (unless encrypted, but that is another story). Fortunately, Git makes it very easy to exclude files from your repository.

Just add a .gitignore file in the root of your project, add rules for files to exclude and you are done. For example the following .gitigore file is typical:

# no PHP storm or other editor stuff
.idea/
*.swp

# No operating system files
.DS_Store

# No unneeded generated stuff
.sass-cache/

# No secrets
secrets

PHPStorm places a .idea/ folder in the root of each project, you do not want that in GIT since your settings may change and developers may have different project-specific settings.

OSX produces these .DS_Store files all over, we do not need them either.

Generated stuff like Sass cache files do not need to be stored in Git since it will be overwritten in the next rendering of your CSS files. Some argue that when using Sass (or Less) you will only have to store your Sass (or .scss of .less) files and generate your CSS using a build process. I like to keep my .css files in Git so I can count in them being there when I need them.

Ignoring the ignore file

Sometimes your generic rules do not apply. In that case, you can ignore those rules. In case you want to share your secrets doe the following:

# I have no secrets
!secrets

Now, secrets will be detected by Git and you can add them.

I want to ignore previously added files

When you go and stop adding secrets in your repo's by adding some rules to your .gitignore file, you need an extra step. Since the files have been added before you need to remove them from Git aswell. Enter the following command to get rid of your secrets in Git:

git rm  --cached secrets

Commit your changes to Git:

git add .
git commit

Now you will see that your secrets are still on disk but are no longer in the repository. Of course, you will need to push your changes to your remote aswell.