在 Windows 上忽略 Git 存储库中的目录
如何在 Windows 上使用 msysgit 忽略 Git 中的目录或文件夹?
How can I ignore directories or folders in Git using msysgit on Windows?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
您可以使用以下内容创建“.gitignore”文件:
它对我有用。
You can create the ".gitignore" file with the contents:
It works for me.
在 Windows 中,斜杠有一个额外的限制。 排除 .gitignore 中的单个目录
可能会工作,但排除所有目录
会导致问题:Git Bash 使用反斜杠转义这些空格(例如
my\ file.txt
) ) 并且 Windows 版 Git 不区分/
和\
。要排除所有目录,最好使用:
两个连续的星号表示目录内容。
In Windows there's an extra catch with slashes. Excluding a single directory in .gitignore with
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 (likemy\ file.txt
) and Git for Windows doesn't distinguish between/
and\
.To exclude all directories, better use:
Two consecutive asterisks signify directory contents.
如果您需要排除子文件夹,您可以使用
**
通配符来排除任何级别的子目录。Just in case you need to exclude sub folders you can use the
**
wildcard to exclude any level of sub directory.另外,在您的
\.git\info
项目目录中,有一个排除文件,它实际上与.gitignore
相同(我认为)。 您可以在其中添加要忽略的文件和目录。Also in your
\.git\info
projects 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.当其他一切都失败时,尝试编辑文件
并将所需的目录添加到文件末尾,如下所示:
我将文件夹“assets”和“compiled”添加到要忽略的文件和目录列表中。
When everything else fails try editing the file
and adding the directories you want to the end of the file, like this:
I added the folders "assets" and "compiled" to the list of files and directories to ignore.
我在让 Git 在 Windows 上获取
.gitignore
文件时遇到了一些问题。$GIT_DIR/info/exclude
文件似乎总是有效。然而,这种方法的缺点是
$GIT_DIR
目录中的文件不包含在签入中,因此不共享。I've had some problems getting Git to pick up the
.gitignore
file on Windows. The$GIT_DIR/info/exclude
file always seems to work though.The downside of this approach, however, is that the files in the
$GIT_DIR
directory are not included in the check-in, and therefore not shared.在 Unix 上:
在 Windows 上:
在终端中执行的这些命令将在当前位置创建一个 .gitignore 文件。
然后只需向此 .gitignore 文件(例如使用 Notepad++)添加应忽略哪些文件或文件夹的信息。 保存您的更改。 就是这样 :)
更多信息:.gitignore
On Unix:
On Windows:
These commands executed in a terminal will create a
.gitignore
file in the current location.Then just add information to this
.gitignore
file (using Notepad++ for example) which files or folders should be ignored. Save your changes. That's it :)More information: .gitignore
我认为问题是您的工作树类似于:
... 以及您描述的 .gitignore 。 这将为您提供
git status
输出,例如:... if
index.html
文件尚未添加到存储库中。 (Git 发现缓存目录中有未忽略的文件,但它只报告目录。)要解决此问题,请确保您已添加并提交index.html
文件:... 和你的 git status 看起来像:(
显然你也想提交 .gitignore 。我只是懒惰了这个测试用例。)
I assume the problem is that your working tree is like:
... with the
.gitignore
you describe. This will give yougit status
output like:... if the
index.html
files have not yet been added to the repository. (Git sees that there are unignored files in the cache directories, but it only reports the directories.) To fix this, make sure that you have added and committed theindex.html
files:... and your
git status
will then look like:(Obviously you do want to commit
.gitignore
as well. I was just being lazy with this test case.)在 Windows 和 Mac 上,如果您想忽略当前目录中名为 Flower_Data_Folder 的文件夹,您可以执行以下操作:
如果它是一个名为
data.txt
的文件:如果它的路径类似于“Data/passwords.txt” ”
On Windows and Mac, if you want to ignore a folder named Flower_Data_Folder in the current directory, you can do:
If it's a file named
data.txt
:If it's a path like "Data/passwords.txt"
我有类似的问题。 我与 Linux 人员一起开发具有共享存储库的 Windows 工具链,他们很乐意在给定文件夹中创建具有相同名称(大小写除外)的文件。
其效果是,我可以克隆存储库并立即拥有数十个“修改过的”文件,如果我签入这些文件,将会造成严重破坏。
我将 Windows 设置为区分大小写,并将 Git 设置为不忽略大小写,但它仍然失败(显然是在 Win32 API 调用中)。
如果我 gitignore 这些文件,那么我必须记住不要跟踪 .gitignore 文件。
但我在这里找到了一个很好的答案:
http://archive.robwilkerson.org/2010/03/02/git-tip-ignore-changes-to-tracked-files/index.html
I had similar issues. I work on a Windows tool chain with a shared repository with Linux guys, and they happily create files with the same (except for case) names in a given folder.
The effect is that I can clone the repository and immediately have dozens of 'modified' files that, if I checked in, would create havoc.
I have Windows set to case sensitive and Git to not ignore case, but it still fails (in the Win32 API calls apparently).
If I gitignore the files then I have to remember to not track the .gitignore file.
But I found a good answer here:
http://archive.robwilkerson.org/2010/03/02/git-tip-ignore-changes-to-tracked-files/index.html
只需在项目文件夹中创建 .gitignore 文件然后在其中添加文件夹的名称,例如:
Just create
.gitignore
file in your project folder Then add the name of the folder in it for ex:这对某些人来说可能非常明显,但我确实从其他答案中理解了这一点。
在目录中创建 .gitignore 文件本身不会执行任何操作。 您必须将 .gitignore 作为文本文件打开,并写入您希望它忽略的文件/目录,每个文件/目录都在自己的行上。
so cd 到 Git 存储库目录
,然后写入要忽略的文件和/或目录的名称及其扩展名(如果相关)。
此外,.gitignore 在某些操作系统(例如 Mac)上是隐藏文件,因此您需要 ls -a 才能看到它,而不仅仅是 ls。
This might be extremely obvious for some, but I did understand this from the other answers.
Making a .gitignore file in a directory does nothing by itself. You have to open the .gitignore as a text file and write the files/directories you want it to ignore, each on its own line.
so cd to the Git repository directory
and then write the names of the files and or directories that you want to be ignored and their extensions if relevant.
Also, .gitignore is a hidden file on some OS (Macs for example) so you need
ls -a
to see it, not just ls.暂时忽略 git 中已经存在的目录/文件:
我在多项目 gradle 项目中有很多项目,它们可能需要很长时间才能删除它们,而且它们都差不多相同但不同。 有时我想通过完全删除它们来从 gradle 构建中删除它们。 毕竟 git 可以把它们找回来。 但是我也不希望它们出现在 git status 中。 所以我使用以下简单的过程;
Temporarily ignore a directory/file that was already in git:
I have a lot of projects in a multi-project gradle project and they can take a long time to delete them, and they're all pretty much the same but different. From time to time I want to remove those from the gradle build by deleting them altogether. git can get them back after all. However I don't want them showing up in git status either. So I use the following simple procedure;
对于那些上述答案不起作用的人。 这个过程非常简单。
For those for whom the above answers didn't work. The process is pretty simple.
在项目目录中创建一个名为
.gitignore
的文件。 通过在文件中输入目录名称(附加斜杠)来忽略目录:更多信息位于此处 。
Create a file named
.gitignore
in your project's directory. Ignore directories by entering the directory name into the file (with a slash appended):More information is here.
默认情况下,Windows 资源管理器将显示
.gitignore
,而实际上文件名是.gitignore.txt
。Git 不会使用
.gitignore.txt
并且您无法将文件重命名为
.gitignore
,因为 Windows 资源管理器认为这是一个没有名称的 gitignore 类型的文件。非命令行解决方案:
By default, Windows Explorer will display
.gitignore
when in fact the file name is.gitignore.txt
.Git will not use
.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.Non command line solution:
似乎忽略文件和目录有两种主要方法:
.gitignore
.gitignore
文件放入存储库的根目录中,除了.git
文件夹(在 Windows 中,请确保 查看真实的文件扩展名 和然后 make.gitignore.
(最后加上点以创建空文件扩展名))~/.gitignore_global
并运行git config --global core.excludesfile ~/.gitignore_global
将其添加到您的 Git 配置取消跟踪
存储库排除 - 对于不需要的本地文件要共享,您只需将文件模式或目录添加到文件
.git/info/exclude
中即可。 这些规则未提交,因此其他用户看不到。 更多信息请参见此处。要在忽略文件列表中设置例外,请参阅此问题。
It seems that for ignoring files and directories there are two main ways:
.gitignore
.gitignore
file into the root of your repository besides the.git
folder (in Windows, make sure you see the true file extension and then make.gitignore.
(with the point at the end to make an empty file extension))~/.gitignore_global
and runninggit config --global core.excludesfile ~/.gitignore_global
to add this to your Git configurationNote: files tracked before can be untracked by running
git rm --cached filename
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.To make exceptions in the list of ignored files, see this question.
要忽略整个目录,请在 .gitignore 中添加“*”。
例如,
示例系统
目标
dirB/ 顶级 (/root/.gitignore)
忽略目录 (/root/dirB/.gitignore)
Git 监视 gitignore在文件系统的每一步。 所以这里我选择dirB/.gitignore为“*”来忽略dirB/,包括其中的所有文件和子目录。
完成☺️
To ignore an entire directory place a .gitignore of “*” there.
For example,
Example System
Goal
Top Level (/root/.gitignore)
Ignored Directory (/root/dirB/.gitignore)
Git watches for gitignore at every step of the file system. So here I choose dirB/.gitignore as “*” to ignore dirB/, including all files and subdirs within.
Done ☺️
要指示 Git 忽略某些文件或文件夹,您必须创建 .gitignore 文件。
但在 Windows 资源管理器中,您必须提供文件的名称。 您只是无法创建仅具有扩展名的文件。 诀窍是创建一个空文本文件,然后转到命令提示符并将文件名更改为
.gitignore
:现在使用您最喜欢的文本编辑器打开该文件并添加您想要的文件/文件夹名称你忽略了。 您还可以使用通配符,如下所示:
*.txt
。To instruct Git to ignore certain files or folders, you have to create
.gitignore
file.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
: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
.我在使用
在 Windows 资源管理器 中创建文件时遇到了一些问题。
在开头。解决方法是进入命令外壳并使用“编辑”创建一个新文件。
I had some issues creating a file in Windows Explorer with a
.
at the beginning.A workaround was to go into the commandshell and create a new file using "edit".
如果你想维护一个文件夹而不是其中的文件,只需在该文件夹中放置一个“.gitignore”文件,并以“*”为内容即可。 该文件将使 Git 忽略存储库中的所有内容。 但是
.gitignore
将包含在您的存储库中。如果添加空文件夹,您会收到此消息(.gitignore 是隐藏文件)
因此,使用“-f”强制添加:
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
.gitignore
will be included in your repository.If you add an empty folder, you receive this message (.gitignore is a hidden file)
So, use "-f" to force add: