Skip to content

Latest commit

 

History

History
119 lines (84 loc) · 3.41 KB

23_gitignore.md

File metadata and controls

119 lines (84 loc) · 3.41 KB

Configure Git to ignore files

While working within your repository, you might notice system or configuration files created by your operating system or text editor. These files are typically things that you don't want git to track or include in the history of your project. Although you can manually ignore those files by never committing them to the history of the project, there is an easier way for you to ignore those files. Git enables you to ignore specific files, files types, and folders through the use of a text file you can add to your repository titled .gitignore. This file instructs Git to stop tracking changes of certain files, which means that these files will not be pushed to GitHub from your local machine.

Example files that you might want Git to ignore:

  1. Secrets, e.g. *.pem, *.key
  2. Binary files, e.g. *.pdf, *.docx
  3. Log files, e.g. *.log
  4. Build files, e.g. Build/*

Readily available .gitignore templates

github/gitignore

The github/gitignore repository is a collection of .gitignore templates based on popular programming languages.

For example, the following snippet shows some suggested files you should start ignoring in a Python project.

python-gitignore

These templates are also available when you create a new repository:

create-gitignore-repo

gitignore.io

This website is another resource for you to find appropriate .gitignore templates when your project contains multiple programming languages.

Activity: Creating a .gitignore file

Ignore files for a single repository

  1. In your Terminal or Git Bash, cd to a repository of interest and create a .gitignore file:

    touch .gitignore

    For Windows users who are not using a bash-based terminal:

    echo >> .gitignore
  2. Go to github/gitignore and choose a template of your choice and paste it in the file.

  3. To share the gitignore rules with other users who clone the repository, commit the .gitignore file to your repository.

    git add .gitignore
    git commit -m "add gitignore file"

Ignore files for all repositories on your local machine

  1. You can also create a global .gitignore file to define a list of rules for ignoring files in every local Git repository.

    To do this, you should be in your root directory:

    touch .gitignore_global
  2. Let's grab an example .gitignore template and paste it in the file:

    # Compiled source #
    ###################
    *.com
    *.class
    *.dll
    *.exe
    *.o
    *.so
    
    # Packages #
    ############
    # it's better to unpack these files and commit the raw source
    # git has its own built in compression methods
    *.7z
    *.dmg
    *.gz
    *.iso
    *.jar
    *.rar
    *.tar
    *.zip
    
    # Logs and databases #
    ######################
    *.log
    *.sql
    *.sqlite
    
    # OS generated files #
    ######################
    .DS_Store
    .DS_Store?
    ._*
    .Spotlight-V100
    .Trashes
    ehthumbs.db
    Thumbs.db
  3. Configure git to use this file:

    For Mac Users:

    git config --global core.excludesfile ~/.gitignore_global

    For Windows Users:

    git config --global core.excludesfile %USERPROFILE%\.gitignore_global