在 Windows 上忽略 Git 存储库中的目录

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

Ignoring directories in Git repositories on Windows

windowsgitgitignoregit-bashmsysgit

提问by sf.

How can I ignore directories or folders in Git using msysgit on Windows?

如何在 Windows 上使用 msysgit 忽略 Git 中的目录或文件夹?

回答by stew

Create a file named .gitignorein your project's directory. Ignore directories by entering the directory name into the file (with a slash appended):

创建一个.gitignore在项目目录中命名的文件。通过在文件中输入目录名称(附加斜杠)来忽略目录:

dir_to_ignore/

More information is here.

更多信息在这里

回答by brainwavedave

By default, Windows Explorer will display .gitignorewhen in fact the file name is .gitignore.txt.

默认情况下,Windows 资源管理器将显示.gitignore实际文件名为.gitignore.txt.

Git will not use .gitignore.txt

Git不会使用 .gitignore.txt

And you can't rename the file to .gitignore, because Windows Explorer thinks it's a file of type gitignore without a name.

并且您不能将该文件重命名为.gitignore,因为 Windows 资源管理器认为它是一个没有名称的 gitignore 类型的文件。

Non command line solution:

非命令行解决方案:

You can rename a file to ".gitignore.", and it will create ".gitignore"

回答by Vairis

It seems that for ignoring files and directories there are two main ways:

似乎忽略文件和目录有两种主要方法:

  1. .gitignore

    • Placing .gitignorefile into the root of your repository besides the .gitfolder (in Windows, make sure you see the true file extensionand then make .gitignore.(with the point at the end to make an empty file extension))
    • Making the global configuration ~/.gitignore_globaland running git config --global core.excludesfile ~/.gitignore_globalto add this to your Git configuration

    Note: files tracked before can be untracked by running git rm --cached filename

  2. Repository exclude- For local files that do not need to be shared, you just add the file pattern or directory to the file .git/info/exclude. Theses rules are not committed, so they are not seen by other users. More information is here.

  1. .gitignore

    • .gitignore文件放入存储库的根目录中,除了.git文件夹(在 Windows 中,确保您看到真实的文件扩展名,然后 make .gitignore.(最后的点可以创建一个空的文件扩展名))
    • 进行全局配置~/.gitignore_global并运行git config --global core.excludesfile ~/.gitignore_global以将其添加到您的 Git 配置中

    注意:之前跟踪的文件可以通过运行取消跟踪 git rm --cached filename

  2. 存储库排除- 对于不需要共享的本地文件,您只需将文件模式或目录添加到文件.git/info/exclude. 这些规则未提交,因此其他用户看不到它们。更多信息在这里

To make exceptions in the list of ignored files, see this question.

要在被忽略文件列表中设置例外,请参阅此问题

回答by sf.

I had some issues creating a file in Windows Explorerwith a .at the beginning.

我有一些问题,创建在一个文件中的Windows资源管理器中有一个.开头。

A workaround was to go into the commandshell and create a new file using "edit".

一种解决方法是进入命令外壳并使用“编辑”创建一个新文件。

回答by Mahes

To instruct Git to ignore certain files or folders, you have to create .gitignorefile.

要指示 Git 忽略某些文件或文件夹,您必须创建.gitignore文件。

But in Windows Explorer you have to provide a name for the file. You just cannot create file with just an extension. The trick is that create a empty text file and go to command prompt and change the name of the file to .gitignore:

但在 Windows 资源管理器中,您必须为文件提供名称。您只是无法创建仅具有扩展名的文件。诀窍是创建一个空文本文件并转到命令提示符并将文件名更改为.gitignore

ren "New Text Document.txt" .gitignore

Now open the file with your favorite text editor and add the file/folder names you wish you ignore. You can also use wildcards like this: *.txt.

现在用您最喜欢的文本编辑器打开文件并添加您希望忽略的文件/文件夹名称。您还可以像这样使用通配符:*.txt.

回答by sensorario

If you want to maintain a folder and not the files inside it, just put a ".gitignore" file in the folder with "*" as the content. This file will make Git ignore all content from the repository. But .gitignorewill be included in your repository.

如果你想维护一个文件夹而不是里面的文件,只需在文件夹中放一个“.gitignore”文件,“*”作为内容。该文件将使 Git 忽略存储库中的所有内容。但.gitignore将包含在您的存储库中。

$ git add path/to/folder/.gitignore

If you add an empty folder, you receive this message (.gitignore is a hidden file)

如果添加空文件夹,则会收到此消息(.gitignore 是隐藏文件)

The following paths are ignored by one of your .gitignore files:
path/to/folder/.gitignore
Use -f if you really want to add them.
fatal: no files added

So, use "-f" to force add:

因此,使用“-f”强制添加:

$ git add path/to/folder/.gitignore -f

回答by wortwart

In Windows there's an extra catch with slashes. Excluding a single directory in .gitignorewith

在 Windows 中有一个额外的斜线捕获。剔除在一个目录中的.gitignore

dir_to_exclude/

dir_to_exclude/

will possibly work, but excluding all directories with

可能会工作,但排除所有目录

/

/

causes problems when you have file names with spaces (like my file.txt) in your directory: Git Bash escapes these spaces with a backslash (like my\ file.txt) and Git for Windows doesn't distinguish between /and \.

当您的目录中的文件名带有空格(如my file.txt)时会导致问题:Git Bash 使用反斜杠(如my\ file.txt)对这些空格进行转义,Windows 版 Git 不区分/\

To exclude all directories, better use:

要排除所有目录,最好使用:

**/

**/

Two consecutive asterisks signify directory contents.

两个连续的星号表示目录内容。

回答by Si3

Also in your \.git\infoprojects directory there is an exclude file that is effectively the same thing as .gitignore(I think). You can add files and directories to ignore in that.

在您的\.git\info项目目录中还有一个排除文件,它实际上与.gitignore(我认为)相同。您可以添加要忽略的文件和目录。

回答by Eat at Joes

Just in case you need to exclude sub folders you can use the **wildcard to exclude any level of sub directory.

万一您需要排除子文件夹,您可以使用**通配符排除任何级别的子目录。

**/build/output/Debug/

回答by J-Dizzle

To ignore an entire directory in Git, the easiest way is to include a .gitignore file within the target directory which simply contains "*".

要忽略 Git 中的整个目录,最简单的方法是在目标目录中包含一个 .gitignore 文件,该文件只包含“*”。

An illustrative example,

一个说明性的例子,

Example System

示例系统

/root/
    .gitignore
    /dirA/
        someFile1.txt
        someFile2.txt
    /dirB/
        .gitignore
        someFile3.txt
        someFile4.txt

Goal

目标

  • ignore the contents of /dirB/
  • 忽略 /dirB/ 的内容

Top Level .gitignore (/root/.gitignore)

顶级 .gitignore (/root/.gitignore)

  • This is where your standard gitignore info goes
  • 这是您的标准 gitignore 信息所在的位置

Ignored Directory .gitignore (/root/dirB.gitignore)

忽略目录 .gitignore (/root/dirB.gitignore)

  • This file just reads as '*' and the directory is ignored completely, itself and all files!
  • 这个文件只是读为“*”,目录被完全忽略,它本身和所有文件!

And it's that simple :)

就这么简单:)