# Ignoring files in Git

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` and`personal 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
	  - [macOS.gitignore](https://github.com/github/gitignore/blob/main/Global/macOS.gitignore), 
	  - [VisualStudioCode.gitignore](https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore) 
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664807303836/Sz0PRco6j.png align="left")
	- 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](https://www.youtube.com/watch?v=buTq5JebsH8)
		- *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](https://github.com/github/gitignore/blob/main/Node.gitignore)
		- config.json (because this contains the token)
			```
			echo config.json >> .gitconfig
			```
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664807713279/mHaZU2YSh.png align="left")
    - 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1664807966127/jRl362r9t.png align="left")
    - ⏩ [Display .git folder on Visual Studio Code](https://www.youtube.com/watch?v=sGqHzNtji6s)

### More .gitignore templates

- Github maintains a collection of [.gitignore templates](https://github.com/github/gitignore/).
- Toptotal [gitignore.io](https://www.toptal.com/developers/gitignore) can be used to generate .gitignore files.





