.gitignore file is a file that tells Git which files or folders it should ignore in a project, so these folders won’t be tracked, staged or committed in the git history.
So if you have been using git and you have been working on projects which you push to GitHub to showcase and store your works, then you would be familiar with the .gitignore file
.gitignore file is usually placed in the root directory of a project and any entries in that file will be ignored in all of your Git repositories.
What Kind of Files Should You Ignore with .gitignore?
- Files that contain AWS key, sensitive credentials, API keys and any other sensitive information
- Dependencies which can be downloaded using package manager e.g npm folders
- Build folders like /dist/
- Any other files that you think ain’t needed for the public to access
Gitignore.io project can save you the time of checking for what you need to add to the .gitignore file, All you need is just input the tech stack that you are using for the project such as your operating system, the programming language, the backend technology and you will get a .gitignore file generated for you, saving us the time.
You can also do it manually, only if you know what and what that needs to be ignored,
Move to the project root folder and create a .gitignore file or do it the Linux way by typing
$ touch .gitignore
Once you have the .gitignore file created, You need to understand the format matching pattern of writing in the .gitignore file.
! Is being used to negate a file that will be ignored i.e it won’t be ignored if others are ignored.
# is used for comments in the file
/ is used for declaring a path of a file or folder pathname in .gitignore
* is used as a wildcard matching, more or less like pick all.
Explanation of how to use .gitignore file
To Ignore Directories
By including, directory paths and putting a “/” at the end will ignore an entire directory e.g
this will ignore the node_modules and logs folder with the entire subfolders and files with them.
Commenting in .gitignore file
Like I made mention earlier, # is for comments in .gitignore file
# macOS Files .DS_Store
the line # macOS Files has nothing to do with the .gitignore file but its a means of making things clear to others in situation where you are not the only one working on a project
The prefix “!” Is used to negate a file that would be ignored;
In the above example, all file ending with .pdf will be ignored except the example.pdf, so the example.pdf file isn’t ignored.
But you can’t try this with files that are inside a directory e.g
The read/example.pdf will still be ignored because the whole directory has already be ignored, so negate is only useful with root files.
This * matches all characters except the /,
e.g, *.pdf matches any file ending with the .pdf extension, but not read/*.pdf
Changing the Global .gitignore
Here is the command to add or change your global .gitignore
$ git config --global core.excludesfile ~/.gitignore_global
A ~/.gitignore_global will be created. Now you can edit this file the same way as a local .gitignore file.
Your Git repositories will ignore the files and folders listed in the global .gitignore file.
What if you’ve already committed a file that needs to be ignored?
Well, git won’t ignore the file since you’ve committed it already, but to get it to be ignored.
You’ll have to untrack the file first, only then will it get ignored, Here’s the command to untrack the file:
git rm --cached FILENAME
.gitignore file is something you’ll come across in almost every project you use git with, and It’s important to always ignore the correct files which you don’t want out there, it will save you from future sensitive info leak.
So you won’t make the same mistake that countless people have made.
If you want to further your understanding on .gitignore check out git documentation book.