如何在不使用 .gitignore 的情况下让 Git 忽略文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/653454/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:16:20  来源:igfitidea点击:

How do you make Git ignore files without using .gitignore?

gitversion-control

提问by pupeno

Due to external weird constraints I cannot modify the .gitignoreof my repository. Is there a way to ignore files and directories other than modifying a .gitignore? Even if it is a global solution like a global configuration that will be applied to all my repositories.

由于外部奇怪的限制,我无法修改.gitignore我的存储库。除了修改.gitignore?之外,有没有办法忽略文件​​和目录?即使它是一个全局解决方案,如将应用于我所有存储库的全局配置。

回答by VonC

Do not forget, according to gitignore, that there is an order of precedence in the different "ignore pattern sources" that Git consider:

不要忘记,根据gitignore,Git 考虑的不同“忽略模式源”有一个优先顺序:

  • Patterns read from the command line for those commands that support them.
  • Patterns read from a .gitignore file in the same directory as the path, or in any parent directory, with patterns in the higher level files (up to the root) being overridden by those in lower level files down to the directory containing the file.
  • Patterns read from $GIT_DIR/info/exclude.
  • Patterns read from the file specified by the configuration variable core.excludesfile.
  • 模式从命令行读取支持它们的那些命令。
  • 从与路径相同的目录或任何父目录中的 .gitignore 文件读取模式,高级文件中的模式(直到根目录)被低级文件中的模式覆盖,直到包含该文件的目录。
  • 模式从$GIT_DIR/info/exclude.
  • 从配置变量指定的文件中读取的模式core.excludesfile

The last two can be a solution for your problem but:

最后两个可以解决您的问题,但是:

  • they are not replicated for a distant repository
  • they can have their patterns overridden by the other sources
  • 它们不会复制到远程存储库
  • 他们可以让其他来源覆盖他们的模式

(See also this SO question)

(另见这个问题



The other two solutions involve updating the index (git update-index):

另外两个解决方案涉及更新索引 ( git update-index):

However, when you checkout another branch or when you git pull, that "ignore" status might be reset. Hence the other option:

但是,当您结帐另一个分支或您git pull时,“忽略”状态可能会被重置。因此,另一种选择:

The difference between the two is explained in "Git - Difference Between 'assume-unchanged' and 'skip-worktree'".

两者之间的区别在“ Git - ' assume-unchanged' 和 ' skip-worktree'之间的区别”中进行了解释。

回答by ólafur Waage

If you can modify .git/info/excludeyou can put the same rules there. But that file is within your local repo only.

如果您可以修改,.git/info/exclude您可以将相同的规则放在那里。但该文件仅在您的本地存储库中。

回答by Ferdinand Beyer

There are three ways to tell GIT which files to ignore:

有 3 种方法告诉 GIT 忽略哪些文件:

  • .gitignorefiles
  • $GIT_DIR/.git/info/exclude
  • Files pointed to via the core.excludesfilesetting
  • .gitignore档案
  • $GIT_DIR/.git/info/exclude
  • 通过core.excludesfile设置指向的文件

The latter two points could solve your problem.

后两点可以解决您的问题。

For further information, see gitignore(5).

有关更多信息,请参阅gitignore(5)

回答by David Winiecki

If you just want to have some local files in the repository and the subdirectory location is flexible, you can put your files in tracked_dir1/tracked_dir2/untracked_dir/and then add a tracked_dir1/tracked_dir2/untracked_dir/.gitignorewith contents like:

如果您只想在存储库中包含一些本地文件并且子目录位置灵活,您可以将文件放入tracked_dir1/tracked_dir2/untracked_dir/,然后添加如下tracked_dir1/tracked_dir2/untracked_dir/.gitignore内容:

*

*

I.e.

IE

$ cat > tracked_dir1/tracked_dir2/untracked_dir/.gitignore
*
<Ctrl+D>

回答by Tony

I have been in similar situations, so I'm adding my preferred solution that I don't see mentioned. The problem with git update-index --assume-unchangedin this case is that you cannot do that for an untracked file. You said

我遇到过类似的情况,所以我添加了我没有看到提到的首选解决方案。git update-index --assume-unchanged在这种情况下,问题在于您不能对未跟踪的文件执行此操作。你说

I cannot modify the .gitignore of my repository.

我无法修改我的存储库的 .gitignore。

I'm going to assume what you mean is that you can't push any changes to .gitignoreto origin. If that is the case what you can do is add the untracked file to your local .gitignore, then do git update-index --assume-unchanged .gitignoreso that your change to .gitignoreis never pushed. Now you are ignoring the (possibly) untracked file, and not affecting the remote .gitignorefile.

我将假设您的意思是您不能将任何更改推.gitignore送到原点。如果是这种情况,您可以做的是将未跟踪的文件添加到您的本地.gitignore,然后这样git update-index --assume-unchanged .gitignore做您的更改.gitignore永远不会被推送。现在您忽略了(可能)未跟踪的文件,并且不会影响远程.gitignore文件。

回答by Elijah Lynn

Ignore local changes to tracked files: git update-index --assume-unchanged my-file.php

忽略对跟踪文件的本地更改: git update-index --assume-unchanged my-file.php

Unignore local changes to tracked files: git update-index --no-assume-unchanged my-file.php

忽略对跟踪文件的本地更改: git update-index --no-assume-unchanged my-file.php

source: git help update-index

来源: git help update-index

--[no-]assume-unchanged
           ...
           This option can be also used as a coarse file-level mechanism to ignore uncommitted changes in tracked
           files (akin to what .gitignore does for untracked files). Git will fail (gracefully) in case it needs to
           modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is
           changed upstream, you will need to handle the situation manually.

回答by langpavel

Another way:

其它的办法:

  • edit local .gitignore
  • then git update-index --assume-unchanged .gitignore
  • 编辑本地 .gitignore
  • 然后 git update-index --assume-unchanged .gitignore

回答by frazras

You can use the global gitignore method instead of modifying the one in the project https://help.github.com/articles/ignoring-files/#create-a-global-gitignore

您可以使用全局 gitignore 方法而不是修改项目中的方法 https://help.github.com/articles/ignoring-files/#create-a-global-gitignore