Create a Global gitignore

Each time you set up a project, there’s that dreaded repetitive task of creating a new .gitignore file and adding in all the same files/directories that need to be ignored.

This sometimes can get pretty tedious. There are OS-generated files (like .DS_Store, Thumbs.db) or IDE-generated files (like .idea/)  that will always clutter up your repository structure. You could also have dependencies like node_modules, built resources like .dll, .so or even logs (*.log) that should never be pushed to the repository.global git ignore

All this repetition could be avoided by setting up a global gitignore file which would ignore the files listed there-in for every repository.

Check for Existing Global gitignore file

Before setting up a new global gitignore file, you should check if it already exists.

# check for existing global .gitignore file
git config --get core.excludesfile
# if configured, should return 'path/fileName' example: '~/.gitignore_global'    

Setup a new Global gitignore file

If not already configured, then you can proceed to configure your .gitignore_global (you can name this anything you want)

# Create file and add files/directories to ignore
nano ~/.gitignore_global               // *nix
edit %USERPROFILE%\.gitignore_global   // Windows (you can use notepad instead)

# Declare the .gitignore_global file
# For *nix systems
git config --global core.excludesfile ~/.gitignore_global
# For Windows
git config --global core.excludesfile %USERPROFILE%\.gitignore_global

# The above command results in an entry in your .gitconfig that looks like this:
[core]
    excludesfile = ~/.gitignore_global                // *nix
    excludesfile = %USERPROFILE%\.gitignore_global    // Windows

The snippet above creates the gitignore_global in your home/user directory thereby eliminating the need to explicitly, repeatedly add the same files and directories to individual .gitignore files! Awesome right!

Remarks

While working in teams, it is possible that someone might unintentionally submit unwanted files/directories only because the repositories own .gitignore did not list those ignored files.

Hence, a good idea is to globally ignore auto-generated files like .DS_Store, Thumbs.db, .idea/, etc but to leave other dependencies like node_modules to be ignored by the local .gitignore file.

As an example, you can check out this .gitignore_global file. Feel free to fork it and use it as your own global .gitignore.


Going Further

  • Setup global gitconfig to ensure consistent configuration between all your repositories. It reduces the redundancy in customizing local gitconfig files on each new project.
  • Using GIT from the command line? don’t forget to setup GIT prompt to view repository details right in your shell.
  • Want to get more productive? See how to use git hooks to automatically lint your code 😉

Happy Tweakin!


References