简体   繁体   English

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

[英]Ignoring directories in Git repositories on Windows

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

Create a file named .gitignore in your project's directory.在项目目录中创建一个名为.gitignore的文件。 Ignore directories by entering the directory name into the file (with a slash appended):通过在文件中输入目录名称来忽略目录(附加斜杠):

dir_to_ignore/

More information is here .更多信息在这里

By default, Windows Explorer will display .gitignore when 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"

It seems that for ignoring files and directories there are two main ways:似乎忽略文件和目录有两种主要方法:

  1. .gitignore .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)).gitignore文件放置到存储库的根目录中,除了.git文件夹(在 Windows 中,确保看到真实的文件扩展名,然后生成.gitignore. (最后的点是空文件扩展名))
    • Making the global configuration ~/.gitignore_global and running git config --global core.excludesfile ~/.gitignore_global to add this to your Git configuration制作全局配置~/.gitignore_global并运行git config --global core.excludesfile ~/.gitignore_global将其添加到您的 Git 配置中

    Note: files tracked before can be untracked by running git rm --cached filename注意:之前跟踪的文件可以通过运行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 .存储库排除- 对于不需要共享的本地文件,您只需将文件模式或目录添加到文件.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 .要在忽略文件列表中设置例外,请参阅此问题

To ignore an entire directory place a .gitignore of “*” there.要忽略整个目录,请在此处放置一个带有“*”的 .gitignore。

For 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 ( /root/.gitignore )顶级/root/.gitignore

  • You could just “ dirB/ “ here你可以在这里“ dirB/

Ignored Directory ( /root/dirB/.gitignore )忽略的目录( /root/dirB/.gitignore )

  • Or you could “*” here或者你可以在这里“*”

Git watches for gitignore at every step of the file system, so if at each level you have something there to ignore, place it in! Git 在文件系统的每一步都监视 gitignore,所以如果在每个级别都有一些东西要忽略,那就把它放进去! (eg placed here as " someFile3.txt ", or in the root .gitignore as " dirB/someFile3.txt " if u want) (例如,在这里放置为“ someFile3.txt ”,或者在根 .gitignore 中放置为“ dirB/someFile3.txt ”,如果你愿意的话)

So here, I choose dirB/.gitignore as “*” to ignore all contents, including all files and subdirs within.所以在这里,我选择 dirB/.gitignore 作为“*”来忽略所有内容,包括其中的所有文件和子目录。

Done ☺️完成☺️

To instruct Git to ignore certain files or folders, you have to create .gitignore file.要指示 Git 忽略某些文件或文件夹,您必须创建.gitignore文件。

But in Windows Explorer you have to provide a name for the file.但在 Windows 资源管理器中,您必须为文件提供名称。 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 :诀窍是创建一个空文本文件并转到命令提示符并将文件名更改为.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

I had some issues creating a file in Windows Explorer with a .我在Windows 资源管理器中使用. at the beginning.一开始。

A workaround was to go into the commandshell and create a new file using "edit".一种解决方法是进入命令外壳并使用“编辑”创建一个新文件。

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.如果你想维护一个文件夹而不是其中的文件,只需在文件夹中放置一个“.gitignore”文件,以“*”作为内容。 This file will make Git ignore all content from the repository.该文件将使 Git 忽略存储库中的所有内容。 But .gitignore will be included in your repository.但是.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

In Windows there's an extra catch with slashes.在 Windows 中,有一个额外的斜线。 Excluding a single directory in .gitignore with排除.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.两个连续的星号表示目录内容。

You can create the ".gitignore" file with the contents:您可以使用以下内容创建“.gitignore”文件:

*
!.gitignore

It works for me.这个对我有用。

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

**/build/output/Debug/

Also in your \.git\info projects directory there is an exclude file that is effectively the same thing as .gitignore (I think).在您的\.git\info项目目录中还有一个排除文件,它实际上与.gitignore相同(我认为)。 You can add files and directories to ignore in that.您可以在其中添加要忽略的文件和目录。

When everything else fails try editing the file一切都失败时,尝试编辑文件

/.git/info/exclude /.git/info/排除

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.我将文件夹“assets”和“compiled”添加到要忽略的文件和目录列表中。

On Unix:在 Unix 上:

touch .gitignore

On Windows:在 Windows 上:

echo > .gitignore

These commands executed in a terminal will create a .gitignore file in the current location.在终端中执行的这些命令将在当前位置创建一个.gitignore文件。

Then just add information to this .gitignore file (using Notepad++ for example) which files or folders should be ignored.然后只需将信息添加到这个.gitignore文件(例如使用 Notepad++)应该忽略哪些文件或文件夹。 Save your changes.保存您的更改。 That's it :)而已 :)

More information: .gitignore更多信息: .gitignore

I've had some problems getting Git to pick up the .gitignore file on Windows.我在让 Git 在 Windows 上获取.gitignore文件时遇到了一些问题。 The $GIT_DIR/info/exclude file always seems to work though. $GIT_DIR/info/exclude文件似乎总是有效。

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.然而,这种方法的缺点是$GIT_DIR目录中的文件不包含在签入中,因此不共享。

ps $GIT_DIR is usually the hidden folder named .git ps $GIT_DIR通常是名为.git的隐藏文件夹

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. ...使用您描述的.gitignore This will give you git status output like:这将为您提供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 the index.html files have not yet been added to the repository. ...如果index.html文件尚未添加到存储库中。 (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 发现缓存目录中有未被忽略的文件,但它只报告目录。)要解决此问题,请确保您已添加并提交index.html文件:

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将如下所示:

$ 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.) (显然你也想提交.gitignore 。我只是懒得处理这个测试用例。)

On Windows and Mac, if you want to ignore a folder named Flower_Data_Folder in the current directory, you can do:在 Windows 和 Mac 上,如果要忽略当前目录中名为 Flower_Data_Folder 的文件夹,可以执行以下操作:

echo Flower_Data_Folder >> .gitignore

If it's a file named data.txt :如果它是一个名为data.txt的文件:

echo data.txt >> .gitignore

If it's a path like "Data/passwords.txt"如果是像“Data/passwords.txt”这样的路径

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

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.我使用与 Linux 人员共享存储库的 Windows 工具链工作,他们很乐意在给定文件夹中创建具有相同(大小写除外)名称的文件。

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).我将 Windows 设置为区分大小写,将 Git 设置为不忽略大小写,但它仍然失败(显然在 Win32 API 调用中)。

If I gitignore the files then I have to remember to not track the .gitignore file.如果我 gitignore 文件,那么我必须记住不要跟踪 .gitignore 文件。

But I found a good answer here:但我在这里找到了一个很好的答案:

http://archive.robwilkerson.org/2010/03/02/git-tip-ignore-changes-to-tracked-files/index.html http://archive.robwilkerson.org/2010/03/02/git-tip-ignore-changes-to-tracked-files/index.html

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

frontend/node_modules

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.在目录中创建 .gitignore 文件本身并没有任何作用。 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.您必须将 .gitignore 作为文本文件打开并写入您希望它忽略的文件/目录,每个文件/目录各占一行。

so cd to the Git repository directory所以 cd 到 Git 存储库目录

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.此外,.gitignore 是某些操作系统(例如 Mac)上的隐藏文件,因此您需要ls -a才能看到它,而不仅仅是 ls。

Temporarily ignore a directory/file that was already in git:暂时忽略 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.我在一个多项目 gradle 项目中有很多项目,它们可能需要很长时间才能删除它们,它们几乎相同但不同。 From time to time I want to remove those from the gradle build by deleting them altogether.有时我想通过完全删除它们来从 gradle 构建中删除它们。 git can get them back after all. git 毕竟可以让他们回来。 However I don't want them showing up in git status either.但是我也不希望它们出现在 git status 中。 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 暂时忽略已删除的文件(我们可以将它们取回)

git ls-files --deleted -z | git ls-files --deleted -z | git update-index --assume-unchanged -z --stdin 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然后运行与以前相同的命令,但将假设未更改为 no-assume-unchanged

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM