How to setup .gitignore file

How to setup .gitignore file
How to setup .gitignore file

The role of .gitignore file is to ignore the changes of the specified file or folder. Say for example if you don’t want to track certain files and their changes throughout the life cycle of the project (example below), we can set up a named .gitignore file list mode to ignore the file. In this article i have listed steps on how to setup local and global .gitignore file.

What files can be .gitignored?

We can .gitignore files that are created as part of platform-specific or automatically created from the build system. Some of  the examples below include 

  1. Application configuration files (eg: Appconfig  , .env)
  2. Build components (ie *.pyc)
  3. System file (ie. Ds_Store for Mac system)
  4. Installed dependencies (ie node_modules)
  5. Personal file (ie todo.txt)
  6. Application data and logs (ie .log, .sql)

Setup .gitignore for a single repository

  1. Open Terminal.
  2. Enter the directory where the project is located.
  3. Create a .gitignore file for your repository and add your rules, then save it as .gitignore.
$ touch .gitignore

.gitignore Patterns

  1. The order of execution of the rules is from top to bottom.
  2. All blank lines and file identification comments starting with # will be ignored by Git.
  3. If the pattern is followed at the end “/” matches only the folder and the continents in the folder and it does not match the file.
  4. If the  pattern at the beginning of “/” matches the item and the directory
  5. ** Match multi-level directories, which can be at the beginning, middle, and end. For example, “**/test” matches file or directory.
  6. *Matches multiple characters.
  7. Use “!” before the mode to ignore files or directories outside the mode.
  8. ? Match a single character.
  9. “[]” matches any character in parentheses

Scenarios where .gitignore will not work?

Sometimes .gitignore will not work. say in case you want  to ignore a file that is already checked in to the repository and you want to ignore it, Git will not ignore the file if you add a rule later. In that scenario, you should untrack the file in the first place by running the following command in your terminal.

# Note: commit all code changes, and then run the above git command.

git rm -r --cached. 
git add.
git commit -m ‘gitignore Updated'

How to set up a global .gitignore file?

It will be tedious to maintain environment-specific .gitignore rules, which might include duplicated patterns and long lists to maintain. The better solution is to go with the Global .gitignore file for all of your repositories. Here is the step to set up one.

  1. Open Terminal.
  2. Use the following command to create a global .gitignore file in your home directory. Run “touch ~/.gitignore_global” in terminal
  3. Add the ignore rules specific to your projects.
  4. Run the below command to configure Git to use the exclude file ~/.gitignore_global for all Git repositories.
$ git config --global core.excludesfile ~/.gitignore_global

Note: The file ~/.gitignore_global can be edited the same way as a local .gitignore file. With global .gitignore all of your git repositories will ignore the folders/files which is listed as part of the file.

.gitignore example

This is an small example (Terraform) of what the .gitignore file for terraform could look like

### Terraform ###
# Local .terraform directories

# .tfstate files

# Crash log files

# Ignore any .tfvars files that are generated automatically for each Terraform run. Most
# .tfvars files are managed as part of configuration and so should be included in
# version control.
# example.tfvars

# Ignore override files as they are usually used to override resources locally and so
# are not checked in

# Include override files you do wish to add to version control using negated pattern
# !

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*

# End of
Read:  what is Gitops? 

.gitignore online generation

We can automatically generate .gitignore files by using the website This website can generate the .gitignore file only by entering the relevant information. In most cases, .gitignore will be generated by default but If you have special needs, you can modify them manually. Refer to one more website having .gitignore templates for your requirement.

My Two cents

1) How to ignore .gitignore file itself

In some scenarios, you want to add a file to Git, but you can’t add it because the file is .gitignore ignored.

$ git add
     The following paths are ignored by one of your .gitignore files:
     Use -f if you really want to add them.

$ git add -v -f

2) To check whether a particular rule is wrong you can use the below command to verify the same

$ git check-ignore -v

To check what .gitignore rule is causing a particular path to be ignored, run git check-ignore:


Git is essential in the development process for developers and master all aspects of git is necessary for improving the development efficiency. .gitignore will be useful for ignoring certain files which are not required to be committed.

Leave a Comment

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

Scroll to Top