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

发布于 2024-07-09 05:42:11 字数 47 浏览 11 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(21

独享拥抱 2024-07-16 05:42:12

您可以使用以下内容创建“.gitignore”文件:

*
!.gitignore

它对我有用。

You can create the ".gitignore" file with the contents:

*
!.gitignore

It works for me.

猫九 2024-07-16 05:42:12

在 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

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 \.

To exclude all directories, better use:

**/

Two consecutive asterisks signify directory contents.

红颜悴 2024-07-16 05:42:12

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

**/build/output/Debug/

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

**/build/output/Debug/
ぃ弥猫深巷。 2024-07-16 05:42:12

另外,在您的 \.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.

暮色兮凉城 2024-07-16 05:42:12

其他一切都失败时,尝试编辑文件

/.git/info/排除

并将所需的目录添加到文件末尾,如下所示:

# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
assets/
compiled/

我将文件夹“assets”和“compiled”添加到要忽略的文件和目录列表中。

When everything else fails try editing the file

/.git/info/exclude

and adding the directories you want to the end of the file, like this:

# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
assets/
compiled/

I added the folders "assets" and "compiled" to the list of files and directories to ignore.

記柔刀 2024-07-16 05:42:12

我在让 Git 在 Windows 上获取 .gitignore 文件时遇到了一些问题。 $GIT_DIR/info/exclude 文件似乎总是有效。

然而,这种方法的缺点是 $GIT_DIR 目录中的文件不包含在签入中,因此不共享。

ps $GIT_DIR 通常是名为 .git 的隐藏文件夹

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.

p.s. $GIT_DIR is usually the hidden folder named .git

谁人与我共长歌 2024-07-16 05:42:12

在 Unix 上:

touch .gitignore

在 Windows 上:

echo > .gitignore

在终端中执行的这些命令将在当前位置创建一个 .gitignore 文件。

然后只需向此 .gitignore 文件(例如使用 Notepad++)添加应忽略哪些文件或文件夹的信息。 保存您的更改。 就是这样 :)

更多信息:.gitignore

On Unix:

touch .gitignore

On Windows:

echo > .gitignore

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

久光 2024-07-16 05:42:12

我认为问题是您的工作树类似于:

a-cache/foo
a-cache/index.html
b-cache/bar
b-cache/foo
b-cache/index.html
.gitignore

... 以及您描述的 .gitignore 。 这将为您提供 git status 输出,例如:

$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    .gitignore
#    a-cache/
#    b-cache/

... if index.html 文件尚未添加到存储库中。 (Git 发现缓存目录中有未忽略的文件,但它只报告目录。)要解决此问题,请确保您已添加并提交 index.html 文件:

git add *cache/index.html
git commit -m "Adding index.html files to the cache directories"

... 和你的 git status 看起来像:(

$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    .gitignore
nothing added to commit but untracked files present (use "git add" to track)

显然你也想提交 .gitignore 。我只是懒惰了这个测试用例。)

I assume the problem is that your working tree is like:

a-cache/foo
a-cache/index.html
b-cache/bar
b-cache/foo
b-cache/index.html
.gitignore

... with the .gitignore you describe. This will give you git status output like:

$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    .gitignore
#    a-cache/
#    b-cache/

... 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 the index.html files:

git add *cache/index.html
git commit -m "Adding index.html files to the cache directories"

... and your git status will then look like:

$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    .gitignore
nothing added to commit but untracked files present (use "git add" to track)

(Obviously you do want to commit .gitignore as well. I was just being lazy with this test case.)

情域 2024-07-16 05:42:12

在 Windows 和 Mac 上,如果您想忽略当前目录中名为 Flower_Data_Folder 的文件夹,您可以执行以下操作:

echo Flower_Data_Folder >> .gitignore

如果它是一个名为 data.txt 的文件:

echo data.txt >> .gitignore

如果它的路径类似于“Data/passwords.txt” ”

echo "Data/passwords.txt" >> .gitignore. 

On Windows and Mac, if you want to ignore a folder named Flower_Data_Folder in the current directory, you can do:

echo Flower_Data_Folder >> .gitignore

If it's a file named data.txt:

echo data.txt >> .gitignore

If it's a path like "Data/passwords.txt"

echo "Data/passwords.txt" >> .gitignore. 
半仙 2024-07-16 05:42:12

我有类似的问题。 我与 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

夏尔 2024-07-16 05:42:12

只需在项目文件夹中创建 .gitignore 文件然后在其中添加文件夹的名称,例如:

frontend/node_modules

Just create .gitignore file in your project folder Then add the name of the folder in it for ex:

frontend/node_modules
明明#如月 2024-07-16 05:42:12

这对某些人来说可能非常明显,但我确实从其他答案中理解了这一点。

在目录中创建 .gitignore 文件本身不会执行任何操作。 您必须将 .gitignore 作为文本文件打开,并写入您希望它忽略的文件/目录,每个文件/目录都在自己的行上。

so cd 到 Git 存储库目录

touch .gitignore
nano .gitignore

,然后写入要忽略的文件和/或目录的名称及其扩展名(如果相关)。

此外,.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

touch .gitignore
nano .gitignore

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.

老旧海报 2024-07-16 05:42:12

暂时忽略 git 中已经存在的目录/文件:

我在多项目 gradle 项目中有很多项目,它们可能需要很长时间才能删除它们,而且它们都差不多相同但不同。 有时我想通过完全删除它们来从 gradle 构建中删除它们。 毕竟 git 可以把它们找回来。 但是我也不希望它们出现在 git status 中。 所以我使用以下简单的过程;

  1. 删除我不想要的文件和文件夹。
  2. 验证构建仍然有效
  3. 告诉 git 暂时忽略已删除的文件(我们可以将它们恢复)

git ls-files --deleted -z | 删除 git update-index --假设不变-z
--标准输入

  1. 在没有目录的情况下继续生活,直到您想要它们回来为止。 然后运行与之前相同的命令,但将假设未更改切换为 no-assume-unchanged

git ls-files --deleted -z | 删除 git update-index --no-assume-unchanged -z
--标准输入

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;

  1. delete files and folders I don't want.
  2. verify build still works
  3. tell git to ignore the deleted files for a bit (we can get them back)


git ls-files --deleted -z | git update-index --assume-unchanged -z
--stdin

  1. go about life without the dirs until you want them back. Then run the same command as before but switch out assume-unchanged for no-assume-unchanged


git ls-files --deleted -z | git update-index --no-assume-unchanged -z
--stdin

紫南 2024-07-16 05:42:12

对于那些上述答案不起作用的人。 这个过程非常简单。

  1. 进入塑料 scm 应用程序并选择工作区资源管理器窗口
  2. 请注意,您要忽略的文件夹有一个绿色勾号 在其上
  3. 右键单击并选择“删除”
  4. 在弹出窗口中,选择“不要从磁盘删除”
  5. 现在只需 <将文件夹名称添加到项目根目录中的ignore.conf文件中

For those for whom the above answers didn't work. The process is pretty simple.

  1. Go into plastic scm app and select your Workspace Explorer window
  2. Notice that the folder you want to ignore has a green tick on it
  3. Right click on it and select "Delete"
  4. In the pop-up window, select "Dont delete from disk"
  5. Now simply add the folder name to ignore.conf file in root of project
一百个冬季 2024-07-16 05:42:11

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

dir_to_ignore/

更多信息位于此处

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

dir_to_ignore/

More information is here.

舂唻埖巳落 2024-07-16 05:42:11

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

Git 不会使用 .gitignore.txt

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

非命令行解决方案:

You can rename a file to ".gitignore.", and it will create ".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:

You can rename a file to ".gitignore.", and it will create ".gitignore"
九歌凝 2024-07-16 05:42:11

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

  1. .gitignore

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

    取消跟踪

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

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

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

  1. .gitignore

    • Placing .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))
    • Making the global configuration ~/.gitignore_global and running git config --global core.excludesfile ~/.gitignore_global to 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.

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

清欢 2024-07-16 05:42:11

要忽略整个目录,请在 .gitignore 中添加“*”。

例如,

示例系统

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

目标

  • 的内容

dirB/ 顶级 (/root/.gitignore)

  • 忽略 这里可以只是“dirB/

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

  • 或者你可以在这里使用“*”

Git 监视 gitignore在文件系统的每一步。 所以这里我选择dirB/.gitignore为“*”来忽略dirB/,包括其中的所有文件和子目录。

完成☺️

To ignore an entire directory place a .gitignore of “*” there.

For example,

Example System

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

Goal

  • ignore the contents of dirB/

Top Level (/root/.gitignore)

  • You could just “dirB/“ here

Ignored Directory (/root/dirB/.gitignore)

  • Or you could “*” here

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 ☺️

高冷爸爸 2024-07-16 05:42:11

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

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

ren "New Text Document.txt" .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:

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.

情深如许 2024-07-16 05:42:11

我在使用 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".

一个人的旅程 2024-07-16 05:42:11

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

$ git add path/to/folder/.gitignore

如果添加空文件夹,您会收到此消息(.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

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

$ git add path/to/folder/.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.

$ git add path/to/folder/.gitignore

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

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:

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