全局 Git 忽略
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7335420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Global Git ignore
提问by Mild Fuzz
I want to set up Git to globally ignore certain files.
我想设置 Git 以全局忽略某些文件。
I have added a .gitignore
file to my home directory (/Users/me/
) and I have added the following line to it:
我已将一个.gitignore
文件添加到我的主目录 ( /Users/me/
) 并添加了以下行:
*.tmproj
But it is not ignoring this type of files, any idea what I am doing wrong?
但它并没有忽略这种类型的文件,知道我做错了什么吗?
回答by CB Bailey
You need to set up your global core.excludesfile
configuration file to point to this global ignore file.
您需要设置您的全局core.excludesfile
配置文件以指向这个全局忽略文件。
e.g.
例如
*nix or Windows git bash:
*nix 或 Windows git bash:
git config --global core.excludesfile '~/.gitignore'
Windows cmd:
视窗命令:
git config --global core.excludesfile "%USERPROFILE%\.gitignore"
Windows PowerShell:
Windows PowerShell:
git config --global core.excludesfile "$Env:USERPROFILE\.gitignore"
For Windows it set to the location C:\Users\{myusername}\.gitignore
. You can verify that the config value is correct by doing:
对于 Windows,它设置为 location C:\Users\{myusername}\.gitignore
。您可以通过执行以下操作来验证配置值是否正确:
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.
结果应该是您的用户配置文件的扩展路径.gitignore
。确保该值不包含未扩展的%USERPROFILE%
字符串。
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)
重要提示:上述命令只会设置 git 将使用的忽略文件的位置。该文件仍必须在该位置手动创建并填充忽略列表。(来自muruge的评论)
You can read about the command at https://help.github.com/articles/ignoring-files/#create-a-global-gitignore
您可以在https://help.github.com/articles/ignoring-files/#create-a-global-gitignore阅读有关该命令的信息
回答by yanni
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那里已经列出了一些东西。因此,对于给定的问题,首先检查现有的排除文件,然后向其中添加新的文件掩码可能是有意义的。
回答by 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:
尽管其他答案是正确的,但它们正在设置全局配置值,而全局 git 忽略文件有一个默认的 git 位置:
*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!
您可能需要创建git
目录和ignore
文件,然后您可以将全局忽略放入该文件中,就是这样!
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 在所有情况下忽略的模式(例如,由用户选择的编辑器生成的备份或临时文件)通常会放入用户的 .git 文件中指定的文件
core.excludesFile
中~/.gitconfig
。它的默认值是 $XDG_CONFIG_HOME/git/ignore。如果 $XDG_CONFIG_HOME 未设置或为空,则使用 $HOME/.config/git/ignore 代替。
回答by Phoera
To create global gitignore from scratch:
从头开始创建全局 gitignore:
$ 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.
- 第一行将目录更改为
C:/Users/User
- 之后,您创建一个带有
.gitignore_global
扩展名的空文件 - 最后将全局忽略设置为该文件。
- 然后你应该用某种记事本打开它并添加所需的忽略规则。
回答by 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 :
如果你在你的 repo 中创建一个名为 .gitignore 的文件,git 在查看要提交的文件时将使用它的规则。请注意,在将规则添加到此文件以忽略它之前,git 不会忽略已跟踪的文件。在这种情况下,必须取消跟踪文件,通常使用:
git rm --cached filename
Is it your case ?
是你的情况吗?
回答by 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.
如果你使用 Unix 系统,你可以用两条命令解决你的问题。其中第一个初始化配置,第二个使用要忽略的文件更改文件。
$ git config --global core.excludesfile ~/.gitignore
$ echo '.idea' >> ~/.gitignore
回答by Prerak Mann
- Create a .gitignore file in your home directory
- 在您的主目录中创建一个 .gitignore 文件
touch ~/.gitignore
- Add files to it (folders aren't recognised)
- 向其中添加文件(无法识别文件夹)
Example
例子
# these work
*.gz
*.tmproj
*.7z
# these won't as they are folders
.vscode/
build/
# but you can do this
.vscode/*
build/*
- Check if a git already has a global gitignore
- 检查一个 git 是否已经有一个全局的 gitignore
git config --get core.excludesfile
- Tell git where the file is
- 告诉 git 文件在哪里
git config --global core.excludesfile '~/.gitignore'
Voila!!
瞧!!
回答by mouse m.d.
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:
只会设置全局文件,但不会创建它。对于 Windows,请检查您的用户目录中的.gitconfig
文件,然后根据您的喜好编辑它。在我的情况下是这样的:
[core]
excludesfile = c:/Users/myuser/Dropbox/Apps/Git/.gitignore
回答by Sri Sankaran
I am able to ignore a .tmproj
file by including either .tmproj
or *.tmproj
in my /users/me/.gitignore-global
file.
我可以.tmproj
通过包含.tmproj
或包含*.tmproj
在我的/users/me/.gitignore-global
文件中来忽略文件。
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.
请注意,文件名.gitignore-global
不是.gitignore
. 通过包含.tmproj
或包含*.tmproj
在目录中调用的文件.gitignore
中,它不起作用/users/me
。
回答by Nic
You should create an exclude file for this. Check out this gistwhich 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.
不过,要解决您的问题,您可能需要.tmproj
使用git rm --cached path/to/.tmproj
, 或git add
和commit
您的.gitignore
文件对文件进行去索引(如果您已将其添加到索引中)。