Skip to content

Ignoring Files in Git. More than just .gitignore

Have you ever needed to exclude a file on your local machine from being committed to your Git repository? There are many reasons you need to do this, including:

  1. Configuration files that contain keys or passwords that shouldn’t be uploaded
  2. External dependencies (like vendor/ and node_modules/) that should be created by build processes.
  3. Temporary storage folders
  4. Log files
  5. Folders with the results of a build process, (you only want your source in the repo).
  6. .DS_Store files (thanks Apple)

There are a few ways to ignore files in Git.

Repo .gitignore file

The first method creating a file in your repo called .gitignore. This file should contain a list of files and folders to ignore when running git status or git commit. Example:

/node_modules
/vendor
.env
*.log

The .gitignore file is committed to your repository and shared with everyone else on the team. This is the recommended approach for project-side rules.

The accepted wildcards for the list of files/folders can be found here:

https://git-scm.com/docs/gitignore

Global .gitignore File

The global .gitignore file affects all repositories running on your local machine. This is a perfect place to add ignore rules related to your IDE, like /.idea for PHPStorm or /.vscode for VSCode.

Create a .gitignore in your home folder on your local machine (or anywhere that makes sense for you). Then set that file as the global ignore file using this command:

git config --global core.excludesFile ~/.gitignore

Personal/Local Git Ignore Rules

There are times you’ll want to exclude files from having changes committed from your local folder to a Git repo, but only for you, not the entire team. For example, my project was using MySQL, but I had to use the MariaDB docker container on my local for Apple M1 support. In order to not accidentally modify the Dockerfile that was committed, I used a local Git rule to ignore the file only for me.

To do this create a file called .git/info/exclude and add the files you want excluded to it in the same format as a .gitignore file.

What if I already committed the file?

If you committed a file you now want to ignore, first, use one of the methods above to ignore the file, and then run:

git rm --cached .DS_Store

This will remove the file (in the example above, .DS_Store) from the repository, but leave it in your local folder.

2 Comments

  1. Hiroki Matsuuchi Hiroki Matsuuchi

    Short, to the point and very useful. Thank you!
    This was my first time hearing about global .gitignore. I work fairly consistently with only a few frameworks/libraries, so setting this up to hide environment variables seems like a great way to prevent myself from making stupid mistakes in the future.
    Thanks for the tip!

    • Shawn Hooper Shawn Hooper

      Agreed! It was being able to globally exclude all the files for my IDE that made that feature worth it.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.