全局 Git 忽略
我想将 Git 设置为全局忽略某些文件。
我已将 .gitignore
文件添加到我的主目录 (/Users/me/
) 中,并向其中添加了以下行:
*.tmproj
但它并没有忽略这种类型文件,知道我做错了什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
在 Linux 的 Windows 子系统上,我必须通过 cd ~/ 然后
touch .gitignore
导航到子系统根目录,然后更新其中的全局 gitignore 配置。我希望它能帮助某人。
on windows subsystem for linux I had to navigate to the subsystem root by
cd ~/
thentouch .gitignore
and then update the global gitignore configuration in there.I hope it helps someone.
您可以使用位于此处的默认全局 gitignore 文件(在 POSIX 系统中):
~/.config/git/ignore
如果您不想更改 git 配置
You can use the default global gitignore file that is located here (in POSIX systems):
~/.config/git/ignore
If you don't want to change your git config
如果 .gitignore 方法不适合您,另一种可能的解决方案是:
这将忽略对该文件的更改(本地和上游),直到您决定再次允许它们:
您可以获得标记为已跳过的文件列表:
请注意,与
--skip-worktree
不同,一旦拉取上游更改,--assume-unchanged
状态就会丢失。Another possible solution if the
.gitignore
approach isn't working for you is to:That will ignore changes to that file, both local and upstream, until you decide to allow them again with:
You can get a list of files that are marked skipped with:
Note that unlike
--skip-worktree
, the--assume-unchanged
status will get lost once an upstream change is pulled.如果您使用 VSCODE,则可以使用此扩展来为您处理任务。
每次您保存工作时,它都会监视您的工作空间,并帮助您自动忽略在 vscode settings.json 中指定的文件和文件夹
忽略(vscode 扩展)
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)
您需要设置全局
core.excludesfile
配置文件以指向此全局忽略文件,例如:*nix 或 Windows git bash:
Windows cmd:
Windows PowerShell:
对于 Windows,它设置为位置
C:\Users\%username%\.gitignore
。您可以通过执行以下操作来验证配置值是否正确:结果应该是用户配置文件的
.gitignore
的扩展路径。确保该值不包含未展开的%USERPROFILE%
字符串。重要:上述命令只会设置 git 将使用的忽略文件的位置。该文件仍必须在该位置手动创建并使用忽略列表填充。 (摘自 muruge 的评论)
您可以在 https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files#configuring-ignored-files-for-all-repositories-on-your-电脑
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:
Windows cmd:
Windows PowerShell:
For Windows it is set to the location
C:\Users\%username%\.gitignore
. You can verify that the config value is correct by doing: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
尽管其他答案是正确的,但它们正在设置全局配置值,而全局 git 忽略文件有一个默认的 git 位置。
您可以通过以下方式检查全局 gitignore 是否存在:
通常,它可以在不同操作系统上的以下位置找到:
*nix:
Windows:
您可能需要创建
git
目录并ignore 文件并通过 git config --global core.excludesfile ~/.config/git/ignore 告诉 git 位置,然后你可以将全局忽略放入该文件中,就是这样!
来源
……
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.
You can check if the global gitignore exists via:
Typically, it is found in these locations on different operating systems:
*nix:
Windows:
You may need to create the
git
directory andignore
file and tell git the location viagit config --global core.excludesfile ~/.config/git/ignore
but then you can put your global ignores into that file and that's it!Source
…
在重新配置全局排除文件之前,您可能需要使用以下命令检查它当前的配置:
在我的例子中,当我运行它时,我看到我的全局排除文件被配置为
and there were already a couple things listed there. So in the case of the given question, it might make sense to first check for an existing excludes file, and add the new file mask to it.
Before reconfiguring the global excludes file, you might want to check what it's currently configured to, using this command:
In my case, when I ran it I saw my global excludes file was configured to
and there were already a couple things listed there. So in the case of the given question, it might make sense to first check for an existing excludes file, and add the new file mask to it.
要从头开始创建全局 gitignore:
C:/Users/User
.gitignore_global
扩展名的空文件To create global gitignore from scratch:
C:/Users/User
.gitignore_global
extension示例
瞧!
Example
Voila!!
来自此处。
如果您在存储库中创建一个名为 .gitignore 的文件,那么 git 在查看要提交的文件时将使用其规则。 请注意,在将规则添加到该文件以忽略它之前,git 不会忽略已经跟踪的文件。 在这种情况下,必须取消跟踪该文件,通常使用:
Is it your case ?
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 :
Is it your case ?
如果你使用Unix系统,两个命令就可以解决你的问题。
第一个初始化配置,第二个使用要忽略的文件更改文件。
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.
请记住,运行该命令
只会设置全局文件,但不会创建它。
对于 Windows,请检查您的用户目录中的
.gitconfig
文件,然后将其编辑为您的首选项。就我而言是这样的:Remember that running the command
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:我可以通过在
/users/me/ 中包含
文件。.tmproj
或*.tmproj
来忽略.tmproj
文件。 gitignore-global请注意,文件名是
.gitignore-global
而不是.gitignore
。通过在/users/me.gitignore
的文件中包含.tmproj
或*.tmproj
不起作用代码>目录。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.您应该为此创建一个排除文件。查看这个要点,这是非常不言自明的。
不过,要解决您的问题,您可能需要使用 git rm --cached path/to/ 取消索引 .tmproj 文件(如果您已将其添加到索引中) .tmproj 或
git add
并提交
您的.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) withgit rm --cached path/to/.tmproj
, orgit add
andcommit
your.gitignore
file.