全局 Git 忽略

发布于 2024-12-03 18:52:55 字数 182 浏览 0 评论 0原文

我想将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

所谓喜欢 2024-12-10 18:52:56

在 Linux 的 Windows 子系统上,我必须通过 cd ~/ 然后 touch .gitignore 导航到子系统根目录,然后更新其中的全局 gitignore 配置。

我希望它能帮助某人。

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.

一梦等七年七年为一梦 2024-12-10 18:52:56

您可以使用位于此处的默认全局 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

青衫负雪 2024-12-10 18:52:56

如果 .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 状态就会丢失。

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.

棒棒糖 2024-12-10 18:52:56

如果您使用 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)

浮华 2024-12-10 18:52:55

您需要设置全局 core.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-电脑

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

狼性发作 2024-12-10 18:52:55

尽管其他答案是正确的,但它们正在设置全局配置值,而全局 git 忽略文件有一个默认的 git 位置。

您可以通过以下方式检查全局 gitignore 是否存在:

git config --get core.excludesfile

通常,它可以在不同操作系统上的以下位置找到:

*nix:

~/.config/git/ignore

Windows:

%USERPROFILE%\.config\git\ignore

您可能需要创建 git 目录并 ignore 文件并通过 git config --global core.excludesfile ~/.config/git/ignore 告诉 git 位置,然后你可以将全局忽略放入该文件中,就是这样!

来源

将模式放置在哪个文件中取决于该模式的用途。

……

  • 用户希望 Git 在所有情况下都忽略的模式(例如,由用户选择的编辑器生成的备份或临时文件)通常会进入用户的 core.excludesFile 指定的文件中代码>~/.gitconfig。它的默认值为 $XDG_CONFIG_HOME/git/ignore。如果 $XDG_CONFIG_HOME 未设置或为空,则使用 $HOME/.config/git/ignore。

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:

git config --get core.excludesfile

Typically, it is found in these locations on different operating systems:

*nix:

~/.config/git/ignore

Windows:

%USERPROFILE%\.config\git\ignore

You may need to create the git directory and ignore file and tell git the location via git config --global core.excludesfile ~/.config/git/ignore 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.
踏雪无痕 2024-12-10 18:52:55

在重新配置全局排除文件之前,您可能需要使用以下命令检查它当前的配置:

git config --get core.excludesfile

在我的例子中,当我运行它时,我看到我的全局排除文件被配置为

~/.gitignore_global

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:

git config --get core.excludesfile

In my case, when I ran it I saw my global excludes file was configured to

~/.gitignore_global

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.

酒儿 2024-12-10 18:52:55

要从头开始创建全局 gitignore:

$ cd ~
$ touch .gitignore_global
$ git config --global core.excludesfile ~/.gitignore_global
  1. 第一行将目录更改为 C:/Users/User
  2. 之后创建一个带有 .gitignore_global 扩展名的空文件
  3. 最后将全局忽略设置为该 文件文件。
  4. 然后您应该使用某种记事本打开它并添加所需的忽略规则。

To create global gitignore from scratch:

$ cd ~
$ touch .gitignore_global
$ git config --global core.excludesfile ~/.gitignore_global
  1. First line changes directory to C:/Users/User
  2. After that you create an empty file with .gitignore_global extension
  3. And finally setting global ignore to that file.
  4. Then you should open it with some kind of notepad and add the needed ignore rules.
仅一夜美梦 2024-12-10 18:52:55
  1. 在你的主目录中创建一个 .gitignore 文件
touch ~/.gitignore
  1. 向其中添加文件/文件夹

示例

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

# Folders
.vscode/
build/

# If folders don't work, you can still do this
.vscode/*
build/*
  1. 检查 git 是否已经有一个全局 gitignore
git config --get core.excludesfile
  1. 告诉 git 文件在哪里
git config --global core.excludesfile '~/.gitignore'

瞧!

  1. Create a .gitignore file in your home directory
touch ~/.gitignore
  1. 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/*
  1. Check if a git already has a global gitignore
git config --get core.excludesfile
  1. Tell git where the file is
git config --global core.excludesfile '~/.gitignore'

Voila!!

呆头 2024-12-10 18:52:55

来自此处

如果您在存储库中创建一个名为 .gitignore 的文件,那么 git 在查看要提交的文件时将使用其规则。 请注意,在将规则添加到该文件以忽略它之前,git 不会忽略已经跟踪的文件。 在这种情况下,必须取消跟踪该文件,通常使用:

git rm --cached filename

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 :

git rm --cached filename

Is it your case ?

病毒体 2024-12-10 18:52:55

如果你使用Unix系统,两个命令就可以解决你的问题。
第一个初始化配置,第二个使用要忽略的文件更改文件。

$ git config --global core.excludesfile ~/.gitignore
$ echo '.idea' >> ~/.gitignore

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
深海不蓝 2024-12-10 18:52:55

请记住,运行该命令

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

只会设置全局文件,但不会创建它。
对于 Windows,请检查您的用户目录中的 .gitconfig 文件,然后将其编辑为您的首选项。就我而言是这样的:

[core]
  excludesfile = c:/Users/myuser/Dropbox/Apps/Git/.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
诗化ㄋ丶相逢 2024-12-10 18:52:55

我可以通过在 /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.

落日海湾 2024-12-10 18:52:55

您应该为此创建一个排除文件。查看这个要点,这是非常不言自明的。

不过,要解决您的问题,您可能需要使用 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) with git rm --cached path/to/.tmproj, or git add and commit your .gitignore file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文