How to configure the .gitignore file

a close up of a computer screen with a bunch of words on it

The .gitignore file is used by Git to determine which files and directories to ignore when committing changes to your repository. This is helpful for preventing unnecessary files (such as temporary files, logs, or sensitive information) from being committed to your Git repository.

To create and configure a .gitignore file, follow these steps:

  1. In the root directory of your project, create a new file named .gitignore (with a period at the beginning of the file name).
  2. Open the .gitignore file in your preferred text editor or IDE (like Visual Studio Code).
  3. Inside the .gitignore file, list the files, directories, or patterns that you want Git to ignore. Each entry should be on a separate line. You can use the following rules for specifying entries:
    • To ignore an entire directory, add a / at the end of the directory name (e.g., node_modules/).
    • To ignore a specific file, just add the file name (e.g., config.env).
    • You can use wildcards for matching patterns (e.g., *.log to ignore all log files).
    • To ignore all files in a directory with a specific extension, use the following syntax: directory_name/*.extension (e.g., logs/*.log).
  4. Save and close the .gitignore file.

Here’s an example of a .gitignore file:

# Ignore log files
# Ignore the secrets script file Ignore the node_modules directory
node_modules/# Ignore all .env files
*.env# Ignore all files in the logs directory with a .log extension

Once you’ve created and configured the .gitignore file, Git will automatically ignore the specified files and directories when you commit changes to your repository.

Remember that if you’ve already committed a file or directory to your repository before adding it to the .gitignore file, you will need to remove the file or directory from the repository and create a new commit before the .gitignore rules will take effect for that file or directory.

Ignore everything

If you want to ignore everything except specific files, directories, or patterns, you can use the ! (exclamation mark) in the .gitignore file to create a whitelist.

Here’s an example of how to configure a .gitignore file to ignore everything except specific items:

# Ignore everything
# Except these specific files or directories

In this example, everything is ignored by default due to the * entry. The ! symbol is then used to negate the ignore rule for specific files or directories.

Please note that if a parent directory is ignored, you cannot unignore a file or folder inside that directory. For example, if you ignore a whole directory but want to allow a specific file inside that directory, you will need to create a more specific rule:

# Ignore everything inside 'my_directory/'
# Except the specific file ‘file1.js’ inside ‘my_directory/’

In this case, everything inside my_directory/ is ignored, but file1.js is not, because we added a more specific rule for it.

You can adjust the .gitignore file according to your needs by listing the files, directories, or patterns that you want to keep in your Git repository.

Leave a Reply

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