Ignoring files in Git

Ignoring files in Git

Global, project and personal

tl;dr: We can tell git to ignore files using .gitignore and exclude.

  • In the home directory, we can set the global .gitignore
  • From within the project repository, there's the project .gitignore andpersonal exclude file (found within the local .git folder).
  • Rules in the subdirectories override the rules in the parent directories.

On .gitignore and exclude files

  1. core.excludesfile

    • Global .gitignore
    • Ignore files across all git repositories of the current user in the system.
    • Example: Files related to the OS and code editor
    • Configure using --global option

      • Below it's named .gitignore but it can be any file name (e.g. .gitignore_global).
      • By convention, this is saved in the same location as the global gitconfig (in the home directory) but it can be in another location.

          nano ~/.gitignore
      • Specify the path to the .gitignore file

          git config --global core.excludesfile ~/.gitignore
      • Additional patterns can be appended using:
          echo .DS_Store >> ~/.gitignore
      • Set global .gitignore
      • It's also possible to set values using --system and the --local options
  2. .gitignore

    • Project .gitignore
    • Ignore files common across all copies of the current project repository.
    • Example: File patterns related to the project and the programming language
      • Node.gitignore
      • config.json (because this contains the token)
          echo config.json >> .gitconfig
    • Version-controlled, committed and shared with anyone who clones the repository.
    • There can be multiple .gitignore in a repository but the convention is to place one in the top level directory. Same level as the local .git folder
  3. .git/info/exclude
    • Personal exclude
    • Ignore files within the current copy of the project repository.
    • Example: File patterns related to personal workflow and development tools
      • Personal commit.template saved in .gitmessage.txt
        echo .gitmessage.txt >> .git/info/exclude
    • exclude is a file located in the local .git folder. This file is not committed to the repository. image.png
    • Display .git folder on Visual Studio Code

More .gitignore templates