As the name indicates, the.gitignore
file, which is a text file, can be used to let Git know which files and directories to exclude/ignore from our git repositories. In other words, it indicates, which files are not necessary to be tracked (to be committed and pushed). Usually, in every codebase we will have some files that need to be or are recommended to be ignored. Most of the time .gitignore
file is created in your repository’s root directory but it could be created as a global file in case we want to ignore something globally in every repository on our machines.
Some of the cases why we don’t want to commit some files in our version control are:
- Mac or Windows system files, such as
.DS_Store
. These files are not really necessary to be on our git repository. .gitignore
can be used to hide API keys and secrets.- Ignore Sass config files.
- Ignored files also could be text files (or .txt). Text files that were created for your notes or reminders.
- Ignore local development environment configuration files. These files are specific to every developer. Therefore no need to be included on your repository.
- Ignore node_module folder.
How to create .gitignore
file?
- Open your Terminal
- cd (Navigate) into your local git repository
- Create
.gitignore
file by typing in your Terminal:$ touch .gitignore
It can also be very common to remove and re-locate already committed file/folder into.gitignore
file. To explain it well, I will provide steps to remove node_module folder as an example:
- Create a .gitignore file in the git repository if it does not contain one
touch .gitignore
2. Open up the .gitignore
and add the following line to the file
node_modules
3. Remove the node_modules folder from the git repository:
git rm -r --cached node_modules
4. Commit the git repository without the node modules folder:
git commit -m "Removed node_module folder"
5. Push the repository to github:
git push origin master
6. After all of that, you should also add the .gitignore
and commit it to the repository:
git add .gitignore
git commit -m "Updated the .gitignore file
git push origin master
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Finally, as mentioned there is a way to configure ignored files globally for all repositories on your computer:
- Open your Terminal
- Type the following command to configure Git for all repositories in your PC.
$ git config --global core.excludesfile ~/.gitignore_global
I hope these instructions will be helpful and exclude some files/folders that you think are not necessary to commit in your Git repository.
More Resouces: