如何为 Windows 设置 .gitignore?

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

How to setup .gitignore for Windows?

windowsgitgithubweb

提问by Mahesh N

I am setting up my first project in Git. How do I setup git-ignorefile in Windows?

我正在 Git 中设置我的第一个项目。如何git-ignore在 Windows 中设置文件?

I am creating my first Rails project using Vagrant and trying to configure .gitignorein Windows

我正在使用 Vagrant 创建我的第一个 Rails 项目并尝试.gitignore在 Windows 中进行配置

回答by Aminah Nuraini

Easy.

简单。

  1. Make a file .gitignoreusing your text editor
  2. In there, write the file name you'd want to ignore
  3. You can use wildcard like: *.pycignoring any file with extension .pyc
  1. .gitignore使用文本编辑器制作文件
  2. 在那里,写下你想忽略的文件名
  3. 您可以使用通配符,例如:*.pyc忽略任何带有扩展名的文件.pyc

If you use TortoiseGit or other Git software, it will be easier. There will be add to ignore listmenu when you right click to a file.

如果你使用 TortoiseGit 或其他 Git 软件,会更容易。add to ignore list右键单击文件时会出现菜单。

回答by sarbada

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

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

.gitignore

.gitignore

  • Placing .gitignorefile into the root of your repo besides .gitfolder (in Windows make sure you see the true file extension and then make .gitignore.

  • Making global configuration ~/.gitignore_globaland running git config --global core.excludesfile ~/.gitignore_globalto add this to your git config

  • .gitignore文件放入除.git文件夹之外的 repo 的根目录中(在 Windows 中,确保您看到真实的文件扩展名,然后将.gitignore.

  • 进行全局配置~/.gitignore_global并运行 git config --global core.excludesfile~/.gitignore_global以将其添加到您的 git 配置中

Note: files tracked before can be untracked by running git rm --cached filename. This absolutely critical for repos that existed BEFORE you created the .gitignore

注意:之前跟踪的文件可以通过运行取消跟踪git rm --cached filename。这对于在您创建之前存在的存储库绝对至关重要.gitignore

Repo exclude

回购排除

For local files that doesn't need to be shared, you just add file pattern or directory to file .git/info/exclude. These rules are not committed, so are not seen by other collaborators in your project. These are machine specific configs.

对于不需要共享的本地文件,只需在 file 中添加文件模式或目录即可.git/info/exclude。这些规则未提交,因此您项目中的其他协作者看不到这些规则。这些是机器特定的配置。

回答by gwh

Windows does not allow you to create a "dotfile" (i.e., a file whose name begins with a dot). There are three simple ways to create a .gitignore in Windows, though. In each case, you want the .gitignore in your project's root directory.

Windows 不允许您创建“点文件”(即名称以点开头的文件)。不过,在 Windows 中可以通过三种简单的方法创建 .gitignore。在每种情况下,您都希望在项目的根目录中使用 .gitignore。

  1. In your text editor, create a file and "Save" or "Save As". Name the file ".gitignore" and the text editor works around the OS's limitation. I use Sublime Text 3 and Vim. This method works for both (Vim would use the command :w, though).

  2. A fallback would be to create an empty (for now) text file and save it as .gitignore.txt then go into the command shell and rename the file to .gitignore (using the command line). That will do it.

  3. Git Bash (available in the git installer for Windows) has the "touch" command and it will create dotfiles. At the Git Bash prompt enter the command "touch .gitignore" and an empty file is created if no file existed with that name.

  1. 在您的文本编辑器中,创建一个文件并“保存”或“另存为”。将文件命名为“.gitignore”,文本编辑器会绕过操作系统的限制。我使用 Sublime Text 3 和 Vim。这种方法对两者都适用(不过,Vim 会使用命令 :w)。

  2. 后备方案是创建一个空的(目前)文本文件并将其另存为 .gitignore.txt,然后进入命令外壳并将文件重命名为 .gitignore(使用命令行)。这样就可以了。

  3. Git Bash(在 Windows 的 git 安装程序中可用)具有“touch”命令,它将创建点文件。在 Git Bash 提示符下输入命令“touch .gitignore”,如果不存在具有该名称的文件,则会创建一个空文件。

Add your exclusion rules inside the .gitignore file:

在 .gitignore 文件中添加排除规则:

  1. Starting point. This repo will give you sample exclusion patterns for (i) Windows, (ii) Rails and (iii) Vagrant. You could add those to a global gitignore file.
  2. GitIgnore / Patterns. This section of the Git Manual explains the patterns to use in your new gitignore file. Basically, you exclude directories and files that don't need version control.
  1. 起点。此存储库将为您提供 (i) Windows、(ii) Rails 和 (iii) Vagrant 的示例排除模式。您可以将它们添加到全局 gitignore 文件中。
  2. GitIgnore/模式。Git 手册的这一部分解释了在新的 gitignore 文件中使用的模式。基本上,您排除不需要版本控制的目录和文件。

回答by Osprey Eagle

To add to previous answers that you should use a text editor to create the .gitignore file, I usually first run dir > .gitignore from a Windows command prompt or Powershell window.

要添加到您应该使用文本编辑器创建 .gitignore 文件的先前答案中,我通常首先从 Windows 命令提示符或 Powershell 窗口运行 dir > .gitignore 。

This outputs the entire directory listing to a file named .gitignore .

这会将整个目录列表输出到名为 .gitignore 的文件中。

Then it's very easy to use a text editor (e.g., Notepad ++ or Atom) to modify the file from the directory listing and not miss a file or mistype a file name.

然后很容易使用文本编辑器(例如,Notepad ++ 或 Atom)从目录列表中修改文件,而不会遗漏文件或错误输入文件名。