ChatGPT解决这个技术问题 Extra ChatGPT

Global Git ignore

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?

You might also want to check out GitHub's ignore suggestions -- help.github.com/articles/ignoring-files; they have a repository of common ignore patterns
You may also want to consider just using a .gitignore inside individual projects. Otherwise if you (or someone else) clone the project on a new system, you'll also have to recreate the global excludesfile and configuration each time.
Ignorable files that the project creates should be in the project .gitignore file, ignorable files that stuff on your machine creates should go in the global .gitignore (like editor/IDE temp files and the like).
Python virtual environment directories are a common use case for entries in my global or local excludesfile.
@Pylinux Actually, per git-scm, personal IDE and workflow specific stuff can go in the untracked file .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.

S
Smart Manoj

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


So long as it's not in your 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 of git status, git config core.excludesfile to your question.
Can I use # at the beginning of lines inside ~/.gitignore to add comments inside the global ignore file?
Recent versions of the official Git Windows toolkit use the *nix syntax instead; if this line doesn't work, try the *nix one instead.
I wasn't able to get this to work using the %USERPROFILE% variable. I had to enter the full path to the file using *nix directory separators. e.g. core.excludesfile C:/Users/User/.gitignore
Couldn't get it working with %USERPROFILE% variable. I had to use git config --global core.excludesfile '~/.gitignore' in windows to get it set to the location C:/users/{myusername}/.gitignore. Also the above command 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.
L
Lawrence

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


There is a default location git looks for this file and 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
S
Steve Jorgensen

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!

Source

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.


This is great; I didn't know about this... however isn't this a generated file? Can it be overwritten when you update/re-install?
I believe the file is created/modified by running certain git commands. Since it is a settings file in each user's directory, I don't think that it should ever be overwritten.
I agree this should be the accepted answer, however I didn't find this in -t̶h̶e̶ ̶d̶o̶c̶u̶m̶e̶n̶t̶a̶t̶i̶o̶n̶ a help article (help.github.com/articles/ignoring-files)
Looks like on Windows the path is also ~/.config/git/ignore.
This is surely the best answer, is there any way to override the accepted one? A lot of people will end up making their lives more complex than they need to if they follow the other answers...
a
alex

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.


P
Prerak Mann

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!!


It seems that name of the file doesn't matter & I found that Kent. C Dodds named this file .gitignore_global, isn't that is the better option eliminating little chance of collision with real .gitignore?
I.m.o it might just be better to append your stuff to the real .gitignore even if it does exist.
L
LaGrandMere

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 ?


I am trying to ignore globally, which is not happening!!
@MildFuzz on Linux the 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/*
P
Purkhalo Alex

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

a
alex

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

S
Sri Sankaran

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.


It doesn't matter what you call your global ignore file so long as it matches your core.excludesfile config.
Ah! Good point @CharlesBailey. However this may be something the writer may want to check. The reason the .tmproj file is not getting ignored may be because the user's excludesfile is not be .gitignore.
I guess he thought the user would be smart enough to realise.
This should have more upvotes, I had the same issue with my git not reconizing the .gitignore globally, just because its name was .gitignore too... So I decided to change it to .gitignore_global and change the "core.excludesfile" to point to that new file and it worked like a charm!
N
Nic

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.


I am trying to do this globally, not just per repo
Yeah, that's what I linked to in the gist. I was a little confused by the original question. Did you do the git rm --cached ?
yeah, tried that. It seems to me I have followed all the recommended advice! What am I missing?
As suggested, I'd add your git status and excludes info to the question :)
b
bboydflo

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.


J
Jose Paez

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.


m
marcdomain

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)