How to Globally Ignore Files in Git
While working in Git, we sometimes need to ignore files we did not need or accidentally commit to the remote repository. We can configure Git to ignore those files we do not want to check into the remote repository.
It will ensure that Git does not track those files and ignore them for future commits from that local computer. Some developers get annoyed by repeatedly copying and pasting the same ignored files for every project they developed for any client.
Git has the ultimate solution of keeping all their IDE-specific and OS files in a global .gitignore
file for this kind of situation. The project’s gitignore
will be used in the future to keep project-specific files.
Globally Ignore Files in Git
To use these commands constantly or globally in the future, we create a .gitignore
file that specifies rules for ignoring files in all of our Git repositories on our local computer.
We develop the file *~/.gitignore_global
and specify some rules related to the future push to the same repository. For this situation, we have to elevate our globalcore.excludesfile
configuration file to locate this global ignore file.
Following are the steps to create a .gitignore
file step by step.
In this first step, we will create a file .gitignore
on our local computer in the path C:\Users\{username}
, for instance, C:\Users\John
. So that the file will be accessible for the project’s next push to the remote repository.
After that, we will adjust the .gitignore
path in three different ways. With its help, we will tell global Git to ignore files in the future push.
We will discuss these three ways one by one with an example below.
Adjust the .gitignore
Path Using Windows Git Bash
First, we will open Windows git bash and write the following command.
git config --global core.excludesFile '~/.gitignore'
Adjust the .gitignore
Path Using Windows CMD
In this way, we will open Windows CMD and write this command.
git config --global core.excludesFile "%USERPROFILE%\.gitignore"
Adjust the .gitignore
Path Using Windows PowerShell
Open Windows PowerShell and write the following command into it.
git config --global core.excludesFile "$Env:USERPROFILE\.gitignore"
Now we can easily set the .gitignore
global file to make our life easier in the future.
Verify the config
File
As we know, all system’s setup is not the same, so for verification for our macOS, Windows, or Windows PowerShell config
file, to see whether it’s correct or not, we will run the following command:
git config --global core.excludesfile
The output will be the full path to your file.
If we see %USERPROFILE%
, then we have a problem.
If we see $HOME/.gitignore_global
or %USERPROFILE%\.gitignore
, something went wrong. On Windows, if we can’t proceed using the %USERPROFILE%
variable, we will run the following command into the command prompt and see the expected answer as well:
git config - global core.excludesfile ~/.gitignore_global
Furthermore, we will go into that folder and open the .gitconfig
file hidden. We will manually edit the excludesfile
path to return to our .gitignore_global
location.
It will look as follows in the below example.
[core]excludesfile = C:\Users\adammcelhaney\.gitignore_global
Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.
LinkedIn