如何创建一个.gitignore文件

1400

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


68
将文本"xxx"写入.gitignore文件。 - ybdesire
12
https://www.gitignore.io/ - Ivan Aracki
13
复制你现有项目中的.gitignore文件。 - Suragch
迁移问题到超级用户怎么样? - Makan
我几年前创建了这个工具:https://github.com/czheo/gitignorepy - czheo
显示剩余5条评论
42个回答

4
有一种非常简单的方法可以创建一个 .gitignore 文件。这个文件可以在 GitHub 上创建,我很确定大多数源代码控制工具都提供了在那里创建文件的功能。
我附上了一份图像逐步教程作为参考。
1. 点击Enter image description here 。 2. 点击Enter image description here 。 3. 点击Enter image description here 。 4. 点击Enter image description here

4

在工作中,我们使用Windows XP,在文件名的末尾键入句点无法生效。一个快速简便的方法来创建.gitignore文件而不会出现“您必须输入文件名”的错误是:

  1. 打开cmd窗口并键入“edit .gitignore”。
  2. 键入“Alt(选择文件菜单),F,S”。现在你有一个空的.gitignore文件,它指向你的cmd提示符。

现在,你可以使用你喜欢的文本编辑器填充它。


4

你看过这篇文章吗?

轻松创建dotnet开发人员的.gitignore文件

它提到:

从.Net Core 3.0开始,你可以通过在终端中键入dotnet new gitignore来利用内置在.Net框架中的gitignore文件模板。

我刚试了一下,它创建的文件如下:

## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore

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

# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs

# Mono auto generated files
mono_crash.*

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
[Ww][Ii][Nn]32/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
[Ll]ogs/

# Visual Studio 2015/2017 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/

# Visual Studio 2017 auto generated files
Generated\ Files/

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

# NUnit
*.VisualState.xml
TestResult.xml
nunit-*.xml

# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c

# Benchmark Results
BenchmarkDotNet.Artifacts/

# .NET
project.lock.json
project.fragment.lock.json
artifacts/

# Tye
.tye/

# ASP.NET Scaffolding
ScaffoldingReadMe.txt

# StyleCop
StyleCopReport.xml

# Files built by Visual Studio
*_i.c
*_p.c
*_h.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*_wpftmp.csproj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc

# Chutzpah Test files
_Chutzpah*

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

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

# Visual Studio Trace Files
*.e2e

# TFS 2012 Local Workspace
$tf/

# Guidance Automation Toolkit
*.gpState

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

# TeamCity is a build add-in
_TeamCity*

# DotCover is a Code Coverage Tool
*.dotCover

# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json

# Coverlet is a free, cross platform Code Coverage Tool
coverage*.json
coverage*.xml
coverage*.info

# Visual Studio code coverage results
*.coverage
*.coveragexml

# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*

# MightyMoose
*.mm.*
AutoTest.Net/

# Web workbench (sass)
.sass-cache/

# 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
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj

# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/

# NuGet Packages
*.nupkg
# NuGet Symbol Packages
*.snupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets

# Microsoft Azure Build Output
csx/
*.build.csdef

# Microsoft Azure Emulator
ecf/
rcf/

# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
*.appxbundle
*.appxupload

# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!?*.[Cc]ache/

# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs

# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk

# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/

# RIA/Silverlight projects
Generated_Code/

# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak

# SQL Server files
*.mdf
*.ldf
*.ndf

# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
*- [Bb]ackup.rdl
*- [Bb]ackup ([0-9]).rdl
*- [Bb]ackup ([0-9][0-9]).rdl

# Microsoft Fakes
FakesAssemblies/

# GhostDoc plugin setting file
*.GhostDoc.xml

# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/

# Visual Studio 6 build log
*.plg

# Visual Studio 6 workspace options file
*.opt

# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw

# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions

# Paket dependency manager
.paket/paket.exe
paket-files/

# FAKE - F# Make
.fake/

# CodeRush personal settings
.cr/personal

# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc

# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config

# Tabs Studio
*.tss

# Telerik's JustMock configuration file
*.jmconfig

# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs

# OpenCover UI analysis results
OpenCover/

# Azure Stream Analytics local run output
ASALocalRun/

# MSBuild Binary and Structured Log
*.binlog

# NVidia Nsight GPU debugger configuration file
*.nvuser

# MFractors (Xamarin productivity tool) working folder
.mfractor/

# Local History for Visual Studio
.localhistory/

# BeatPulse healthcheck temp database
healthchecksdb

# Backup folder for Package Reference Convert tool in Visual Studio 2017
MigrationBackup/

# Ionide (cross platform F# VS Code tools) working folder
.ionide/

# Fody - auto-generated XML schema
FodyWeavers.xsd

##
## Visual studio for Mac
##


# globs
Makefile.in
*.userprefs
*.usertasks
config.make
config.status
aclocal.m4
install-sh
autom4te.cache/
*.tar.gz
tarballs/
test-results/

# Mac bundle stuff
*.dmg
*.app

# content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
[Dd]esktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp

# Windows shortcuts
*.lnk

# JetBrains Rider
.idea/
*.sln.iml

##
## Visual Studio Code
##
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

3

我知道这个问题着重于如何“创建”gitignore文件,但是如果有人想快速地向已创建的文件中添加内容,以下是我的答案,使用GitHub Desktop来“忽略”出现在更改列表中的文件。

  1. 对生成仓库中不需要的更改的代码进行更改。
  2. 进入GitHub桌面版并进入您的仓库。
  3. 选择所有更改并右键点击它们。
  4. 将更改添加到gitignore文件中。

Enter image description here


3

在Mac上 -

您只需创建一个新的文本文件。

使用https://www.gitignore.io/添加内容

将文件保存为 - 富文本文档附件格式。

更改文件名为 .gitingore,并在弹出窗口中选择使用“。”,如附图所示。

注意:由于它是一个隐藏文件,您将无法在目录中看到它。但是它会被创建。

查看图片


2

我使用Notepad++。使用菜单文件新建文件另存为.gitignore保存类型所有文件(.)


1

是的,Windows资源管理器不允许您创建此文件名。另一个简单的方法是在目录中创建一个虚拟文件,例如NewFile.txt,然后只需在Git Bash中像下面这样重命名它:

mv NewFile.txt .gitignore

这在 macOS 终端上可以工作。 - Guy West

0

你可以让Xcode在启动新项目时自动生成它。

为了这样做,你需要开始进行一些无害的黑客攻击...

在开始之前:备份“项目模板”,因为我预测你会想做更多的事情,一旦你发现了它。

现在,进入/Developer/Library/Xcode/Project Templates/Application/Cocoa Application/,将你的.gitignore文件添加到那里。

就是这样。当你创建一个新的“Cocoa Application”项目时,.gitignore文件会自动从你的项目模板中复制过来。

注意:如果你想编辑模板本身,请使用nano;不要使用Xcode或TextEdit,因为它们会搞乱Unicode字符!好吧,Xcode也会搞乱其他所有东西。

注:还有一个“文件模板”,在你开始修改它们之前,你也应该备份它们。同样:使用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,但它会不断地在我的控制台上发送垃圾信息)。

0

在 Mac 上:

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

  • 重新启动 Finder 以便查看隐藏文件:command + option + escape重新启动

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


0
Ubuntu中,您可以创建.gitignore文件并添加(忽略)当前目录.中所有超过50 MB的文件,使用以下命令:
find . -size +100M | cat >> .gitignore

这不仅限于Ubuntu。 - Peter Mortensen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接