git 如何创建 .gitignore 文件

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

How to create a .gitignore file

gitxcode4.3gitignore

提问by Luca

I need to add some rules to my .gitignorefile. However, I can't find it in my project folder. Isn't it created automatically by Xcode? If not, what command allows me to create one?

我需要在我的.gitignore文件中添加一些规则。但是,我在我的项目文件夹中找不到它。它不是由Xcode自动创建的吗?如果没有,什么命令允许我创建一个?

回答by ajcw

If you're using Windows it will not let you create a file without a filename in Windows Explorer. It will give you the error "You must type a file name" if you try to rename a text file as .gitignore

如果您使用的是 Windows,它不会让您在 Windows 资源管理器中创建没有文件名的文件。如果您尝试将文本文件重命名为.gitignore,它会给您错误“您必须输入文件名

enter image description here

enter image description here

To get around this I used the following steps

为了解决这个问题,我使用了以下步骤

  1. Create the text file gitignore.txt
  2. Open it in a text editor and add your rules, then save and close
  3. Hold SHIFT, right click the folder you're in, then select Open command window here
  4. Then rename the file in the command line, with ren gitignore.txt .gitignore
  1. 创建文本文件 gitignore.txt
  2. 在文本编辑器中打开它并添加您的规则,然后保存并关闭
  3. 按住 SHIFT,右键单击您所在的文件夹,然后选择在此处打开命令窗口
  4. 然后在命令行中重命名文件,使用 ren gitignore.txt .gitignore

Alternatively @HenningCashsuggests in the comments

或者@HenningCash在评论中建议

You can get around this Windows Explorer error by appending a dot to the filename without extension: .gitignore. will be automatically changed to .gitignore

您可以通过在不带扩展名的文件名后附加一个点来解决此 Windows 资源管理器错误:.gitignore。将自动更改为 .gitignore

回答by kaiser

As simple as things can (sometimes) be: Just add the following into your preferred command line interface (GNU Bash, Git Bash, etc.)

事情可以(有时)尽可能简单:只需将以下内容添加到您喜欢的命令行界面(GNU Bash、Git Bash 等)

touch .gitignore

As @Wardy pointed out in the comments, touchworks on Windows as well as long as you provide the full path. This might also explain why it does not work for some users on Windows: The touchcommand seems to not be in the $PATHon some Windows versions per default.

正如@Wardy 在评论中指出的那样,touch只要您提供完整路径,就可以在 Windows 上运行。这也可以解释为什么它对Windows上的某些用户不起作用:默认情况下,该touch命令似乎不在$PATH某些 Windows 版本中。

C:\> "c:\program files (x86)\git\bin\touch.exe" .gitignore

回答by chos

The easiest way to create the .gitignore file in Windows Explorer is to create a new file named .gitignore.. This will skip the validation of having a file extension, since is actually has an empty file extension.

在 Windows 资源管理器中创建 .gitignore 文件的最简单方法是创建一个名为 .gitignore 的新文件.gitignore.。这将跳过具有文件扩展名的验证,因为实际上具有空文件扩展名。

回答by sully6768

The .gitignore file is not added to a repository by default. Use vi or your favorite text editor to create the .gitignore file then issue a git add .gitignorefollowed by git commit -m "message" .gitignore. The following commands will take care of it.

默认情况下,.gitignore 文件不会添加到存储库中。使用vi或您喜爱的文本编辑器来创建那么的.gitignore文件发出git add .gitignore之后git commit -m "message" .gitignore。以下命令将处理它。

> .gitignore
git add .gitignore
git commit -m "message" .gitignore

回答by sully6768

========== In Windows ==========

========== 在 Windows 中 ==========

  1. Open Notepad.
  2. Add the contents of your gitignore file.
  3. Click "Save as" and select "all files".
  4. Save as .gitignore.
  1. 打开记事本
  2. 添加 gitignore 文件的内容。
  3. 单击“另存为”并选择“所有文件”。
  4. 另存为.gitignore.

======== Easy peasy! No command line required! ========

======== 容易peasy!不需要命令行!========

回答by Suragch

MacOS / Linux one-liner

MacOS / Linux 单线

An easy way to get a default git ignore without messing about with create/copy/paste is to use the curlcommand from the terminal. First cdinto your projects root directory and then run the command by replacing MY_API_NAMEwith your API name from one of the following two sources:

获得默认 git ignore 而不会搞乱创建/复制/粘贴的一种简单方法是使用curl终端中的命令。首先cd进入您的项目根目录,然后通过替换MY_API_NAME来自以下两个来源之一的 API 名称来运行命令:

gitignore.io

gitignore.io

curl -o .gitignore https://www.gitignore.io/api/MY_API_NAME

You can find your API name by searching from the list hereand clicking Generate.

您可以通过从此处的列表中搜索并单击生成来找到您的 API 名称。

Java Example:

Java 示例:

curl -o .gitignore https://www.gitignore.io/api/java

GitHub

GitHub

Alternatively you can use the ones at GitHub. Find the filename for your API here.

或者,您可以使用 GitHub 上的那些。在此处查找 API 的文件名。

curl -o .gitignore https://raw.githubusercontent.com/github/gitignore/master/MY_API_NAME.gitignore

Java Example:

Java 示例:

curl -o .gitignore https://raw.githubusercontent.com/github/gitignore/master/Java.gitignore

Windows

视窗

Here are some similar alternatives for Windows.

以下是 Windows 的一些类似替代方案。

But honestly setting that up looks like more trouble that it is worth. If I had Windows then I would just create an empty file called .gitignorein my project's root folder and then copy and paste the default text from gitignore.io or GitHub.

但老实说,设置它看起来更麻烦,但值得。如果我有 Windows,那么我只需.gitignore在我的项目的根文件夹中创建一个空文件,然后从 gitignore.io 或 GitHub 复制并粘贴默认文本。

回答by seay

On Windows you can use cmd echo "" >.gitignore

在 Windows 上,您可以使用 cmd echo "" >.gitignore

Or use Git Bash cmd touch .gitignore, this useful for Linux and Mac System

或者使用 Git Bash cmd touch .gitignore,这对 Linux 和 Mac 系统很有用

回答by Oo.oO

I want my contribution as well. This time, animated one :)

我也想要我的贡献。这一次,动画一个:)

VIM (mini tutorial):

VIM(迷你教程):

i   - start editing
ESC - get back to normal mode
:w  - save 
:q  - quit

enter image description here

enter image description here

回答by Roberto Rodriguez

Using Git Bash console.

使用 Git Bash 控制台。

-Navigate to your project -Type "touch .gitignore"

- 导航到您的项目 - 输入“touch .gitignore”

the .gitignore file will be created for you.

.gitignore 文件将为您创建。

enter image description here

enter image description here

回答by tanguy_k

Here a nice tip under Windows:

这是 Windows 下的一个很好的提示:

  • Right click in Windows Explorer, New > Text Document
  • Name it .gitignore. (with a trailing dot - that is the tip)
  • You end up with a .gitignore file :)
  • 在 Windows 资源管理器中右键单击,新建 > 文本文档
  • 将其命名为 .gitignore。(带有尾随点 - 这是小费)
  • 你最终得到一个 .gitignore 文件:)

Tested under Windows 7 and 8.

在 Windows 7 和 8 下测试。

This tip assumes that your Windows Explorer displays the file extensions.

此技巧假设您的 Windows 资源管理器显示文件扩展名。

Windows Explorer .gitignore

Windows Explorer .gitignore