I want to set up Git to globally ignore certain files.
I have added a .gitignore
file to my home directory (/Users/me/
) and I have added the following line to it:
*.tmproj
But it is not ignoring this type of files, any idea what I am doing wrong?
.git/info/exclude
in the repo, so it doesn't necessarily have to go in the global file. Also, the default and automatic global gitignore file is $HOME/.config/git/ignore
.
You need to set up your global core.excludesfile
configuration file to point to this global ignore file e.g:
*nix or Windows git bash:
git config --global core.excludesFile '~/.gitignore'
Windows cmd:
git config --global core.excludesFile "%USERPROFILE%\.gitignore"
Windows PowerShell:
git config --global core.excludesFile "$Env:USERPROFILE\.gitignore"
For Windows it is set to the location C:\Users\%username%\.gitignore
. You can verify that the config value is correct by doing:
git config --global core.excludesFile
The result should be the expanded path to your user profile's .gitignore
. Ensure that the value does not contain the unexpanded %USERPROFILE%
string.
Important: The above commands will only set the location of the ignore file that git will use. The file has to still be manually created in that location and populated with the ignore list. (from muruge's comment)
You can read about the command at https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files#configuring-ignored-files-for-all-repositories-on-your-computer
Before reconfiguring the global excludes file, you might want to check what it's currently configured to, using this command:
git config --get core.excludesfile
In my case, when I ran it I saw my global excludes file was configured to
~/.gitignore_global
git config --get core.excludesFile
does not return it, unfortunately. What is currently "configured" if nothing is returned from that is something like ~/.config/git/ignore
. See this answer below: stackoverflow.com/a/22885996/9849440
Although other answers are correct they are setting the global config value whereas there is a default git location for the global git ignore file:
*nix:
~/.config/git/ignore
Windows:
%USERPROFILE%\git\ignore
You may need to create git
directory and ignore
file but then you can put your global ignores into that file and that's it!
Which file to place a pattern in depends on how the pattern is meant to be used.
…
Patterns which a user wants Git to ignore in all situations (e.g., backup or temporary files generated by the user’s editor of choice) generally go into a file specified by core.excludesFile in the user’s ~/.gitconfig. Its default value is $XDG_CONFIG_HOME/git/ignore. If $XDG_CONFIG_HOME is either not set or empty, $HOME/.config/git/ignore is used instead.
git
commands. Since it is a settings file in each user's directory, I don't think that it should ever be overwritten.
~/.config/git/ignore
.
To create global gitignore from scratch:
$ cd ~
$ touch .gitignore_global
$ git config --global core.excludesfile ~/.gitignore_global
First line changes directory to C:/Users/User After that you create an empty file with .gitignore_global extension And finally setting global ignore to that file. Then you should open it with some kind of notepad and add the needed ignore rules.
Create a .gitignore file in your home directory
touch ~/.gitignore
Add files/folders to it
Example
# Files
*.gz
*.tmproj
*.7z
# Folders
.vscode/
build/
# If folders don't work, you can still do this
.vscode/*
build/*
Check if a git already has a global gitignore
git config --get core.excludesfile
Tell git where the file is
git config --global core.excludesfile '~/.gitignore'
Voila!!
.gitignore_global
, isn't that is the better option eliminating little chance of collision with real .gitignore?
From here.
If you create a file in your repo named .gitignore git will use its rules when looking at files to commit. Note that git will not ignore a file that was already tracked before a rule was added to this file to ignore it. In such a case the file must be un-tracked, usually with :
git rm --cached filename
Is it your case ?
core.excludesfile
file seems to be interpreted differently than a .gitignore
file. If you want to ignore an entire directory just put the name of the directory like .vscode/
instead of .vscode/*
If you use Unix system, you can solve your problem in two commands. Where the first initialize configs and the second alters file with a file to ignore.
$ git config --global core.excludesfile ~/.gitignore
$ echo '.idea' >> ~/.gitignore
Remember that running the command
git config --global core.excludesfile '~/.gitignore'
will just set up the global file, but will NOT create it. For Windows check your Users directory for the .gitconfig
file, and edit it to your preferences. In my case It's like that:
[core]
excludesfile = c:/Users/myuser/Dropbox/Apps/Git/.gitignore
I am able to ignore a .tmproj
file by including either .tmproj
or *.tmproj
in my /users/me/.gitignore-global
file.
Note that the file name is .gitignore-global
not .gitignore
. It did not work by including .tmproj
or *.tmproj
in a file called .gitignore
in the /users/me
directory.
core.excludesfile
config.
.tmproj
file is not getting ignored may be because the user's excludesfile
is not be .gitignore
.
You should create an exclude file for this. Check out this gist which is pretty self explanatory.
To address your question though, you may need to either de-index the .tmproj
file (if you've already added it to the index) with git rm --cached path/to/.tmproj
, or git add
and commit
your .gitignore
file.
git rm --cached
?
on windows subsystem for linux I had to navigate to the subsystem root by cd ~/
then touch .gitignore
and then update the global gitignore configuration in there.
I hope it helps someone.
Another possible solution if the .gitignore
approach isn't working for you is to:
git update-index --skip-worktree path_to_file
That will ignore changes to that file, both local and upstream, until you decide to allow them again with:
git update-index --no-skip-worktree path_to_file
You can get a list of files that are marked skipped with:
git ls-files -v . | grep ^S
Note that unlike --skip-worktree
, the --assume-unchanged
status will get lost once an upstream change is pulled.
If you're using VSCODE, you can get this extension to handle the task for you. It watches your workspace each time you save your work and helps you to automatically ignore the files and folders you specified in your vscode settings.json ignoreit (vscode extension)
Success story sharing
HEAD
or your index it shouldn't make any difference whether the file was once tracked or not. It may be helpful if you add the output ofgit status
,git config core.excludesfile
to your question.#
at the beginning of lines inside~/.gitignore
to add comments inside the global ignore file?%USERPROFILE%
variable. I had to enter the full path to the file using *nix directory separators. e.g. core.excludesfile C:/Users/User/.gitignore