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
- Application configuration files (eg: Appconfig , .env)
- Build components (ie *.pyc)
- System file (ie. Ds_Store for Mac system)
- Installed dependencies (ie node_modules)
- Personal file (ie todo.txt)
- Application data and logs (ie .log, .sql)
Setup .gitignore for a single repository
- Open Terminal.
- Enter the directory where the project is located.
- Create a .gitignore file for your repository and add your rules, then save it as .gitignore.
$ touch .gitignore
- The order of execution of the rules is from top to bottom.
- All blank lines and file identification comments starting with # will be ignored by Git.
- 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.
- If the pattern at the beginning of “/” matches the item and the directory
- ** Match multi-level directories, which can be at the beginning, middle, and end. For example, “**/test” matches file or directory.
- *Matches multiple characters.
- Use “!” before the mode to ignore files or directories outside the mode.
- ? Match a single character.
- “” 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.
- Open Terminal.
- Use the following command to create a global .gitignore file in your home directory. Run “touch ~/.gitignore_global” in terminal
- Add the ignore rules specific to your projects.
- 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.
This is an small example (Terraform) of what the .gitignore file for terraform could look like
### Terraform ### # Local .terraform directories **/.terraform/* # .tfstate files *.tfstate # Crash log files crash.log # 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 override.tf override.tf.json *_override.tf *_override.tf.json # Include override files you do wish to add to version control using negated pattern # !example_override.tf # Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan # example: *tfplan* # End of https://www.toptal.com/developers/gitignore/api/terraform
.gitignore online generation
We can automatically generate .gitignore files by using the website https://gitignore.io/. 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 pytest.py The following paths are ignored by one of your .gitignore files: pytest.py Use -f if you really want to add them. $ git add -v -f pytest.py
2) To check whether a particular rule is wrong you can use the below command to verify the same
$ git check-ignore -v pytest.py
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.