我想设置Git全局忽略某些文件。

我已经添加了一个.gitignore文件到我的主目录(/Users/me/),我已经添加了以下行:

*.tmproj

但它并没有忽略这种类型的文件,知道我做错了什么吗?


当前回答

记住,运行命令

git config --global core.excludesfile '~/.gitignore'

只会设置全局文件,而不会创建全局文件。 对于Windows,检查Users目录下的.gitconfig文件,并根据您的喜好编辑它。对我来说是这样的:

[core]
  excludesfile = c:/Users/myuser/Dropbox/Apps/Git/.gitignore

其他回答

你需要建立你的全球核心。Excludesfile配置文件指向这个全局忽略文件,例如:

*nix或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"

对于Windows,它被设置为C:\Users\%username%\.gitignore。您可以通过执行以下操作来验证配置值是否正确:

git config --global core.excludesFile

结果应该是用户配置文件.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-computer上阅读有关该命令的信息

如果.gitignore方法不适合你,另一个可能的解决方案是:

git update-index --skip-worktree path_to_file

这将忽略对该文件的更改,包括本地和上游,直到您决定再次使用以下命令允许它们:

git update-index --no-skip-worktree path_to_file

你可以得到一个被标记为跳过的文件列表:

git ls-files -v . | grep ^S

注意,与——skip-worktree不同的是,——assume-unchanged状态将在上游更改被拉出后丢失。

从头开始创建全局gitignore:

$ cd ~
$ touch .gitignore_global
$ git config --global core.excludesfile ~/.gitignore_global

第一行更改目录为C:/Users/User 之后,创建一个扩展名为.gitignore_global的空文件 最后设置全局忽略到那个文件。 然后,您应该使用某种记事本打开它,并添加所需的忽略规则。

在主目录中创建一个.gitignore文件

touch ~/.gitignore

向其中添加文件/文件夹

例子

# Files
*.gz
*.tmproj
*.7z

# Folders
.vscode/
build/

# If folders don't work, you can still do this
.vscode/*
build/*

检查git是否已经有一个全局gitignore

git config --get core.excludesfile

告诉git文件在哪里

git config --global core.excludesfile '~/.gitignore'

瞧! !

在linux的windows子系统上,我必须通过cd ~/导航到子系统根目录,然后触摸.gitignore,然后在那里更新全局gitignore配置。

我希望它能帮助到一些人。