A global .gitignore file allows you to specify patterns for files and directories that should be ignored by Git across all repositories on your system.

A colleague of mine was erroneously placing files in their merge requests that weren’t relevant, and I took the opportunity to educate them as well as share the knowledge more broadly, even if some consider it very basic information.

macOS and .DS_Store

macOS creates a .DS_Store file inside any directory you view with Finder. This has always annoyed me about using a Mac for casual editing. If you consult man git-config, you will come across a method to ignore this across all repositories instead of modifying each one with a .gitignore.

core.excludesFile -
Specifies the pathname to the file that contains patterns to describe paths that are not meant to be tracked, in addition to .gitignore (per-directory) and .git/info/exclude. Defaults to $XDG_CONFIG_HOME/git/ignore. If $XDG_CONFIG_HOME is either not set or empty, $HOME/.config/git/ignore is used instead. See gitignore(5).

Success! This is exactly what we need.

Here is a step-by-step guide to create a global .gitignore file:

  1. Create the .config directory and the accompanying ignore file. Since this is macOS, you will not have any of the XDG variables, so we use the fallback paths instead.

     $ mkdir -p ~/.config/git/
     $ touch ~/.config/git/ignore
    
  2. Add the patterns for the files you want to ignore, in this case the dreaded .DS_Store files. You can also take this opportunity to include other things you wish to ignore globally. For example, .swp files come to mind if you are a heavy Vim user.

    Contents of ~/.config/git/ignore:

     # Ignore macOS system files
     .DS_Store
    
  3. Verify things are working by checking for the presence of a .DS_Store in a git directory.

     % ls -1A
     .DS_Store
     .git
     .github
     .gitmodules
     .submodules
     .vscode
     COPYING
     Containerfile.ArchLinux
     Containerfile.Fedora
     Containerfile.UbuntuLTS
     Containerfile.template
     README.md
     devcontainer.json.template
    
     % git status
     On branch main
     Your branch is up to date with 'origin/main'.
    
     nothing to commit, working tree clean
    

    No .gitignore for this repository and no pending changes!

Wrapping Up

This wasn’t a particularly exciting post, but it was something that had cropped up working with a junior engineer. Sometimes it is easy to forget that I was once green too. Educating others instead of judging is the better path forward. Something about rising tides and boats comes to mind.