Git It Right The First Time With .gitignore

The best thing you can do with Git is stop bad files from ever being committed.

That is exactly what .gitignore is for.

The first thing you should do in a new repo is configure .gitignore. It is a day‑one Git habit. It tells Git, “These files live on my machine, not in source control.”

Once you understand that, you avoid cleanup, embarrassment, and security problems later.

What Is .gitignore?

.gitignore is a plain text file in your repository.

Inside it, you list files and folders Git should ignore.

Ignored files:

  • Are not tracked
  • Are not committed
  • Are not pushed to GitHub

This is how you keep junk, secrets, and machine‑specific files out of your repo.

Why You Use .gitignore

Some files should never be committed:

  • IDE files from IntelliJ, VS Code, and others
  • Build artifacts
  • Local config files
  • Credentials
  • Keys, certificates, and PEM files
  • Environment files like .env

Once sensitive files hit GitHub, the damage is done.

.gitignore helps stop that before it happens.

When You Use .gitignore

Use .gitignore as soon as you create a repo.

Best practice:

  • Add it before your first commit
  • Update it when you introduce new tools or files

If a file is already committed .gitignore will not remove it.

That is why this should be done early.

.gitignore only applies to untracked files.

If you already committed something by mistake:

git rm --cached filename

Then add it to .gitignore.

That’s it.

How You Use .gitignore

Create a file named exactly:

.gitignore

Then list files or patterns Git should ignore.

Example: IDE files

.idea/

.vscode/

*.iml

Example: Credentials and keys

.env

*.pem

*.key

credentials.json

Example: OS junk

.DS_Store

Thumbs.db

Git reads this file automatically. No extra setup required.

Where It Lives

The .gitignore file lives at the root of your repository.

You can create it with:

  • VS Code
  • IntelliJ
  • Any text editor

You can also start with prebuilt templates for:

  • Java
  • Python
  • Node
  • Terraform
  • Docker

They save time and help you avoid missing important files.

One Last Tip

.gitignore should not optional, it’s basic Git hygiene.

If you have ever asked:

  • Why is my repo full of junk?
  • How did secrets get pushed?
  • Why are there merge conflicts in IDE files?

There are other things you can do to prevent bad commits. I will cover those in my upcoming blogs. 

Comments

Leave a Reply