如何创建.gitignore文件

时间:2012-05-24 19:48:55

标签: git xcode4.3 gitignore

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

42 个答案:

答案 0 :(得分:1541)

如果您使用的是Windows,则不会让您在Windows资源管理器中创建没有文件名的文件。如果您尝试将文本文件重命名为 .gitignore

,它将为您提供错误“您必须输入文件名

enter image description here

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

  1. 创建文本文件gitignore.txt
  2. 在文本编辑器中打开它并添加规则,然后保存并关闭
  3. 按住SHIFT,右键单击您所在的文件夹,然后选择在此处打开命令窗口
  4. 然后使用ren gitignore.txt .gitignore
  5. 在命令行中重命名该文件

    或者@HenningCash在评论中建议

      

    您可以通过添加点来解决此Windows资源管理器错误   没有扩展名的文件名:.gitignore。将自动   改为.gitignore

答案 1 :(得分:444)

事情可以(有时)简单:只需将以下内容添加到首选命令行界面(GNU Bash,Git Bash等)

touch .gitignore

testmenow

更新:使用Windows CMD测试,但在Win 8.1+上无效。不过它可以使用Git Bash,如果你使用的是Git,你应该在Windows中安装。

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

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

答案 2 :(得分:283)

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

答案 3 :(得分:165)

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

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

答案 4 :(得分:91)

==========在WINDOWS中==========

  1. 打开记事本。
  2. 添加gitignore文件的内容。
  3. 点击“另存为”,然后选择“所有文件”。
  4. 另存为.gitignore
  5. ========轻松腻!不需要命令行! ======

答案 5 :(得分:42)

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

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

答案 6 :(得分:42)

MacOS / Linux one-liner

在不弄乱创建/复制/粘贴的情况下获取默认git忽略的简单方法是使用终端中的curl命令。首先cd进入项目根目录,然后通过将MY_API_NAME替换为以下两个来源之一的API名称来运行命令:

gitignore.io

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

您可以通过在列表here中搜索并点击生成来找到您的API名称。

Java示例:

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

GitHub

或者你可以使用GitHub上的那些。找到API here的文件名。

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

Java示例:

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

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

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

答案 7 :(得分:34)

使用Git Bash控制台。

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

将为您创建.gitignore文件。

enter image description here

答案 8 :(得分:30)

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

VIM(迷你教程):

oc patch -p $(cat file.patch) ...

enter image description here

答案 9 :(得分:26)

这是Windows下的一个不错的提示:

  • 在Windows资源管理器中右键单击,新建>文字文件
  • 将其命名为.gitignore。 (带尾点 - 即提示)
  • 你最终获得了.gitignore文件:)

在Windows 7和8下测试。

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

Windows Explorer .gitignore

答案 10 :(得分:19)

我的贡献针对的是Mac上的那些,它不仅适用于那些在iOS项目上工作的人(如提到Xcode的问题所暗示的那样),而且适用于任何类型的项目。

<小时/>
我这样做的简单方法是进入终端并运行vim .gitignore然后添加文件。通常,您可以在https://github.com/github/gitignore

上从Github上的一个模板中复制您需要的内容


第1步
在您的项目中运行

vim .gitnore

enter image description here



第2步
您现在已使用vim打开文件。

enter image description here

i插入文字。当您在底部看到 - INSERT - 时,您会看到该文件已准备就绪。

enter image description here



第3步(选项1)
对于Objective-C项目,您可以从https://raw.githubusercontent.com/github/gitignore/master/Objective-C.gitignore进行复制并将其粘贴到.gitignore

enter image description here

然后按esc,然后输入:wq,然后按return。哪个保存文件。



第3步(选项2)
添加适用于您的项目的任何文件。

如果您不确定要添加什么,在搜索引擎中使用的最佳关键字将包括您的项目类型和文本编辑器。例如,如果您使用Sublime,则需要添加

*.sublime-workspace

如果您在Dreamweaver中使用Cordova项目,则需要添加

_notes
dwsync.xml

答案 11 :(得分:13)

创建.gitignore文件,包括您不想提交的所有文件和目录。

例如:

#################
## Eclipse
#################

*.pydevproject
.project
.metadata
.gradle
bin/
tmp/
target/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.classpath
.settings/
.loadpath

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# CDT-specific
.cproject

# PDT-specific
.buildpath


#################
## Visual Studio
#################

## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

# User-specific files
*.suo
*.user
*.sln.docstates

# Build results

[Dd]ebug/
[Rr]elease/
x64/
build/
[Bb]in/
[Oo]bj/

# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*

*_i.c
*_p.c
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.log
*.scc

# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opensdf
*.sdf
*.cachefile

# Visual Studio profiler
*.psess
*.vsp
*.vspx

# Guidance Automation Toolkit
*.gpState

# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper

# TeamCity is a build add-in
_TeamCity*

# DotCover is a Code Coverage Tool
*.dotCover

# NCrunch
*.ncrunch*
.*crunch*.local.xml

# Installshield output folder
[Ee]xpress/

# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html

# Click-Once directory
publish/

# Publish Web Output
*.Publish.xml
*.pubxml

# NuGet Packages Directory
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
#packages/

# Windows Azure Build Output
csx
*.build.csdef

# Windows Store app package directory
AppPackages/

# Others
sql/
*.Cache
ClientBin/
[Ss]tyle[Cc]op.*
~$*
*~
*.dbmdl
*.[Pp]ublish.xml
*.pfx
*.publishsettings

答案 12 :(得分:13)

在Windows中,打开dos提示符(cmd)窗口,使用命令行:

type > .gitignore

答案 13 :(得分:13)

我有另一个简单的想法 让我们在cmd中使用echo命令,   

echo ./idea > .gitignore

这将创建具有文本内容的.gitignore文件&#34; ./ idea&#34;
您现在可以使用文本编辑器手动更改文件中的数据。

或只是

console:

  

echo .gitignore notepad.exe

立即编辑gitignore。

如果您不知道哪些文件应该为您的IDE或操作系统进行gitignored,请转到www.gitignore.io

  

gitignore.io - here it wll generate the gitignore commands or text for you just say your api or os thats it!. Just copy and paste into your file. simple!

答案 14 :(得分:12)

这是我个人最喜欢的,http://help.github.com/ignore-files/

另外,如果您想忽略xcode文件,请参阅此答案https://stackoverflow.com/a/49488/126235

答案 15 :(得分:11)

http://gitignore.io 是一个开源实用程序,可以帮助您为项目创建有用的.gitignore文件。您还可以通过gi命令访问命令行API:http://gitignore.io/cli

  1. OSX 安装gi命令:

    $ echo "function gi() { curl http://gitignore.io/api/\$@ ;}" >> ~/.bash_profile && source ~/.bash_profile

  2. 查看.gitignore文件内容(输出:http://gitignore.io/api/xcode,osx):

    $ gi xcode,osx

  3. 如果要将结果附加到新的.gitignore文件,您应该在终端上看到输出。

    $ gi xcode,osx >> .gitignore

答案 16 :(得分:10)

使用cmd创建.gitignore的几种方法:

  • 使用copy con命令:

    1. 打开 cmd 并向您的git存储库说明cd
    2. copy con .gitignore并按 Ctrl + Z

enter image description here

  • 使用start notepad .gitignore命令:

    1. 打开 cmd 并向您的git存储库说明cd
    2. start notepad .gitignore并在打开的记事本对话框中按按钮。

enter image description here

  • 使用edit .gitignore命令(仅限Windows x86):

    1. 打开 cmd 并向您的git存储库说明cd
    2. edit .gitignore并关闭已打开的编辑编辑器。

答案 17 :(得分:9)

以下适用于Powershell和命令提示符

echo '*.ignore_me' > .gitignore

我遇到了一个奇怪的问题,git实际上无法读取.gitignore文件。然后我删除了.gitignore文件并使用vim创建了一个工作正常。

要添加要忽略的其他文件,只需调用以下命令:

echo 'another_file_to_ignore' >> .gitignore

它会将更多文件追加到现有的.gitignore文件中。

答案 18 :(得分:8)

1)创建.gitignore文件,为此,您只需创建一个.txt文件并按以下方式更改扩展名: enter image description here

然后你必须改变在cmd上写下面一行的名称:

 rename git.txt .gitignore

其中git.txt是您刚刚创建的文件的名称。

然后,您可以打开该文件并编写您不想在存储库中添加的所有文件。例如我的看起来像这样:

#OS junk files
[Tt]humbs.db
*.DS_Store

#Visual Studio files
*.[Oo]bj
*.user
*.aps
*.pch
*.vspscc
*.vssscc
*_i.c
*_p.c
*.ncb
*.suo
*.tlb
*.tlh
*.bak
*.[Cc]ache
*.ilk
*.log
*.lib
*.sbr
*.sdf
*.pyc
*.xml
ipch/
obj/
[Bb]in
[Dd]ebug*/
[Rr]elease*/
Ankh.NoLoad

#Tooling
_ReSharper*/
*.resharper
[Tt]est[Rr]esult*

#Project files
[Bb]uild/

#Subversion files
.svn

# Office Temp Files
~$*

一旦你有了这个,你需要将它添加到你的git存储库。您必须将文件保存到存储库所在的位置。

然后在你的git bash中你必须写下以下一行:

enter image description here

如果存储库已经存在,那么您必须执行以下操作:

1)git rm -r --cached。 2)git add。 3)git commit -m“.gitignore正在工作”

如果第2步不起作用,那么你应该编写你想要添加的文件的孔路径。

希望它有所帮助!

答案 19 :(得分:7)

如果您不想让.gitignore干扰其他人的存储库,您也可以使用.git/info/exclude。 (见http://help.github.com/ignore-files/

答案 20 :(得分:6)

Windows enter image description here

  

文件名:“。gitignore”,   保存类型:所有文件

答案 21 :(得分:6)

您可以转到https://www.toptal.com/developers/gitignore

选择IDE,操作系统或编程语言。 它将自动为您生成。 enter image description here

答案 22 :(得分:6)

To force Finder to display hidden files and folders via Terminal:

    Open Terminal
    For OS X 10.9 Mavericks, run this command (lower-case finder):
    defaults write com.apple.finder AppleShowAllFiles TRUE
    For OS X 10.8 Mountain Lion, 10.7, or 10.6, run this command (upper-case Finder):
    defaults write com.apple.Finder AppleShowAllFiles true
    notice the setting for true
    Then run this command: killall Finder
    Then exit Terminal
    To revert back to Finder’s default setting (hide hidden files and folders), 
run the opposite command but with the false setting.

然后运行killall Finder并退出终端。

答案 23 :(得分:6)

不使用命令行

  1. 打开texteditor并添加规则。
  2. 点击文件 - &gt;另存为
  3. 将其另存为&#34; .gitignore&#34; (包括引文)

答案 24 :(得分:6)

1. Open git terminal
2. go to git repository of the project
3. create a .gitignore file by **touch .gitignore** command
4. **git add .gitignore** command to add ignore file
5. set ignore rules in the ignore file
6. run the command **cat .gitignore**

通过在步骤3中运行命令,您将获得项目目录中的.gitignore文件。 感谢。

答案 25 :(得分:5)

windows: 在命令行中:

.>.gitignore

这将显示错误,但可以正常工作

答案 26 :(得分:4)

您可以在Windows Powershell中键入new-item .gitignore

答案 27 :(得分:4)

您可以直接创建一个空的.gitignore文件,在需要添加该文件的位置打开cmd并键入以下命令

copy con .gitignore

ENTER 您现在处于新创建文件的编辑模式下,但是我们现在不需要添加任何内容,只需按F6,然后按ENTER 现在您有了一个空的.gitignore文件,请使用您拥有的任何编辑器编辑文件

答案 28 :(得分:4)

在工作中,我们使用的是Windows XP,在文件名末尾键入句点不起作用。在没有“您必须输入文件名”错误的情况下创建.gitignore文件的快速简便方法是:

  1. 打开cmd窗口并输入“edit .gitignore”。
  2. 键入“Alt(选择文件菜单),F,S。现在,只要cmd提示符指向,就会有一个空的.gitignore文件。
  3. 您现在可以使用自己喜欢的文本编辑器填充它

答案 29 :(得分:3)

在mac中 - 你可以创建一个新的文本文件。 使用添加内容 https://www.gitignore.io/

将文件格式保存为 - 带附件的富文本文档。 将文件名更改为.gitingore 当弹出窗口出现在附加图像中时选择use"."

注意:因为它是一个隐藏文件,所以你无法在目录中看到它。但它会被创建。

see image

答案 30 :(得分:3)

如果您使用Sublime作为IDE,则可以创建一个新文件并将其另存为.gitignore。只需使用ctrl N作为新文件,然后按ctrl S保存为“.gitignore”。

答案 31 :(得分:3)

要在Xcode中添加任何文件,请转到菜单并导航至 档案 - &gt;新 - &gt;文件...

对于.gitignore文件,请选择其他 - &gt;清空并单击“下一步”。 在“另存为”字段中键入名称(.gitignore),然后单击“创建”。 对于以点(“。”)开头的文件,将弹出一条警告消息,告知您该文件将被隐藏。只需点击“使用”。继续...

就是这样。

要填写全新的.gitignore,您可以在此处找到忽略Xcode文件的示例:https://stackoverflow.com/a/12021580/2120348

答案 32 :(得分:2)

我使用记事本++。新文件&gt; SaveAs&gt; .gitignore(保存类型 - &gt;所有类型())

答案 33 :(得分:2)

有一种非常简单的方法来创建.gitignore文件,该文件是在github中创建的,我敢肯定大多数源代码控件都提供了在其自身中创建文件的功能。

1。enter image description here

2。enter image description here

3。enter image description here

4。enter image description here

答案 34 :(得分:2)

我意识到这个问题集中在如何“创建” gitignore文件上,但是如果有人对在创建文件后向其添加内容的快速方法感兴趣,这是我为那些试图“忽略”的人提供的答案出现在其更改列表中的文件。

  1. 对代码进行更改,从而在存储库中生成不需要的更改。
  2. 转到GitHub Desktop和您的存储库。
  3. 选择所有更改,然后右键单击它们。
  4. 将更改添加到gitignore文件。

enter image description here

答案 35 :(得分:1)

这是我的盟友 - 但是,我仍在努力弄清楚一些.dll是如何进入的:

http://www.gitignore.io/ http://www.diaryofaninja.com/blog/2011/06/01/how-to-easily-create-a-gitignore-file-inside-windows-explorer

希望这有帮助!

答案 36 :(得分:0)

是的,Windows资源管理器不允许您创建此文件名。解决这个问题的另一个简单方法是在目录中创建一个虚拟文件,例如NewFile.txt,而不是简单地在git bash中重命名它,如下所示:

mv NewFile.txt .gitignore

答案 37 :(得分:0)

我还没有提到的一件事是,当你开始一个新项目时,你可以让Xcode自动生成它。 为了做到这一点,你必须自己开始做一些无害的黑客攻击......

开始之前:备份&#34;项目模板&#34;,因为我预测一旦你发现了它,我想要做的比我刚刚提到的要多。

现在,转到/ Developer / Library / Xcode / Project Templates / Application / Cocoa Application / 在那里添加.gitignore文件。

这就是全部。当你创建一个新的&#34; Cocoa应用程序&#34;项目,然后.gitignore文件将自动从项目模板中复制。

请注意是否要自行编辑模板。使用 nano ;不要使用Xcode或TextEdit,它们搞乱了unicode字符! -Well Xcode也搞砸了其他一切。

注意:还有一个&#34;文件模板&#34;,在开始修改它们之前,您还应该备份它们。再次:使用 nano 进行编辑;不是Xcode,也不是TextEdit。

这是我自己的.gitignore文件之一,您可以将其用作灵感:

.DS_Store
Icon\15
Icon\r
Icon\n
/*.xcodeproj/*.mode*
/*.xcodeproj/*.pbxuser
/*.xcodeproj/TemplateIcon.icns
/*.xcodeproj/.LSOverride
!/*.xcodeproj/default.*
/*.pbproj/*.mode*
/*.pbproj/*.pbxuser
/*.pbproj/*.perspective*
/build/
*.moved-aside
*~.nib
*~.xib

注意:我使用Xcode 2.5和Xcode 3.1.4(更喜欢3.1,但它会不断地向我的控制台发送垃圾邮件)

答案 38 :(得分:0)

在Mac上:

  • 打开终端并在任何地方运行defaults write com.apple.finder AppleShowAllFiles YES

  • 重新启动查找器,以便您可以查看隐藏的文件:command + option + escape ---> Relaunch

然后创建一个文本文件,您将能够将文件扩展名更改为.gitignore

答案 39 :(得分:0)

如果已全局安装git,请运行以下命令。如果不是,请先安装git并将git添加到系统变量路径

SELECT p.name AS promotion,
       c.name AS client,
       COALESCE(MAX(l.points), 'None') AS points
FROM promotion p
JOIN client c ON c.id = p.client_id
LEFT JOIN level l ON l.promotion_id = p.id AND l.level < c.value
GROUP BY p.name, c.name

答案 40 :(得分:0)

要将.gitignore文件添加到非应用程序中,可以使用

npx add-gitignore

现在,您可以键入“节点”,并使用用户空格键选择它并按Enter。这会将节点.gitignore添加到项目中。 enter image description here

答案 41 :(得分:-1)

这是Windows中的Linux“ touch”的单一衬里版本

c:\<folder>\break > .gitignore

这将创建一个空白的.gitignore文件,您可以在其中编辑和添加要忽略的项目

C:\Users\test>dir .gitignore
 Volume in drive C is Windows
 Volume Serial Number is 9223-E93F

 Directory of C:\Users\test

18/04/2019  02:23 PM                 0 .gitignore
               1 File(s)              0 bytes
               0 Dir(s)  353,009,770,496 bytes free

C:\Users\test>