While working on your projects, you will often find many ignore files and directories (like .env or any other local environment secrets, .vscode or any different IDE configurations, .DS_store MacOS related, .eslintrc, .csslintrc, node_modules/ and the list goes on ) that you have to add every time in your repository .gitignore. And obviously, you don’t want to track and push them to Git because firstly, a repository should not care about any personal or environmental configurations and secondly it would be a bit of difficult to maintain .gitignore rules on the repository level.
There is an easy and better way to solve this issue that Git provides us with, which is to set up a personal global .gitignore file at your home directory level. You can list all your ignore files and directories from all of your repositories (whether new or old).
.gitignore for Mac OS Users
Open your terminal and in your home directory run the following command to create a new file.
Now you need to open it with the text editor of your choice and you can add in there whatever folder/files you always want to ignore in every repository.
For example to open your file in VSCode after the above command run this.
I am adding quite a few global configurations for my repositories which look like this:
The final step would be to configure it with Git so that it can use our ~/.gitignore file for all of our Git repositories. You will need to run the following command in your terminal.
.gitignore for Windows OS Users
Create a .gitignore file in the root directory of your user profile. You can also achieve this by using Git Bash and then running the following command for configurations.
This is it. Now you don’t have to worry about those troublesome configurations for every repository individually.
Note: – You can also use the following command on any system to verify the config file.
This should return the full path of your file. If not then there must be something wrong with the configuration.
Note: – For more details about .gitignore, you can also refer to the Git documentation
For a full list of handy Git commands you can check git commands cheat sheet