前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >说说gitlab中的.gitignore文件

说说gitlab中的.gitignore文件

原创
作者头像
软件架构师Michael
发布2024-08-06 13:31:25
1200
发布2024-08-06 13:31:25
举报
文章被收录于专栏:软件工程师Michael

作为一名资深开发工程师或者架构师,非常有必要了解一些运费方面的知识。

前不久给一个6年工作经验的朋友修改了求职简历,原始简历中提到了自己未来的定位是做一名架构师。可是在技能清单部分完全没有出现运维的相关技术栈。于是给他加了几个常用的运维的技术点。后来,那位朋友表示不太理解。对技术的认知停留在这个层面,说明他距离真正的架构师还有较长的路要走。

回到正题。

前几天遇到在gitlab客户端拉去代码时,有提示字符串过长的报错提示。这里就不截图了。事后查资料,发现这个时比较常见的错误。解决的办法是只要在.git同级目录下添加一个.gitignore文件,就可以了。

以下是.gitignore 文件的具体内容:

代码语言:txt
复制
## 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/
[Oo]ut/
[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 Core
project.lock.json
project.fragment.lock.json
artifacts/

# 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/
dist/

# 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

首先,了解 .gitignore 文件的原理和作用是非常重要的。.gitignore 文件是 Git 版本控制系统中用来指定哪些文件或目录应该被 Git 忽略,不加入版本控制的配置文件。下面是 .gitignore 文件的一些关键点:

1. **作用**:

- 避免将不需要版本控制的文件(如编译生成的文件、日志文件、个人配置文件等)加入到 Git 仓库中。

- 减少仓库的大小,提高版本控制的效率。

- 保护敏感信息,防止误将敏感文件加入版本控制。

2. **原理**:

- `.gitignore` 文件本身需要被加入到版本控制中,这样其他开发者可以知道哪些文件是被忽略的。

- Git 会读取 `.gitignore` 文件中的规则,并根据这些规则来确定哪些文件应该被忽略。

- 规则可以是文件名、目录名、文件扩展名,也可以是复杂的模式匹配。

3. **语法规则**:

- 以斜杠 `/` 开头的表示目录。

- 以星号 `*` 表示任意字符的匹配。

- 以问号 `?` 表示单个字符的匹配。

- 以方括号 `[]` 表示字符范围匹配,如 `[a-z]` 匹配所有小写字母。

- 使用 `!` 来排除规则,即不忽略匹配到的文件。

4. **使用场景**:

- 忽略操作系统生成的文件,如 `.DS_Store`(macOS)。

- 忽略编译生成的文件,如 `*.class`、`*.exe` 等。

- 忽略日志文件,如 `*.log`。

- 忽略个人配置文件,如 `config.local`。

5. **注意事项**:

- 如果某个文件已经被 Git 跟踪(即已经被添加到版本控制中),那么即使该文件在 `.gitignore` 中被指定为忽略,它仍然会被版本控制,除非使用 `git rm --cached <file>` 命令来移除。

- `.gitignore` 文件的规则是递归的,只对当前目录及其子目录有效。

- 在项目根目录下创建 `.gitignore` 文件,以全局忽略文件。

通过合理地使用 `.gitignore` 文件,可以有效地管理项目中的文件,避免将不必要的文件加入版本控制,同时也保护了项目的安全性和隐私性。

在使用 `.gitignore` 文件时,确实存在一些常见的错误或误解,以下是一些需要避免的点:

1. **忽略已跟踪文件**:

- 如果文件已经被 Git 跟踪,仅仅在 `.gitignore` 中添加规则是不够的。需要使用 `git rm --cached <file>` 来从 Git 索引中删除该文件。

2. **忽略父目录规则**:

- 如果在子目录中创建了 `.gitignore` 文件,该文件只能忽略该子目录下的文件,而不能忽略父目录中的文件。

3. **忽略规则的覆盖**:

- 规则是按顺序匹配的,如果一个规则被后续的规则覆盖,那么前面的规则将不会生效。例如,`*.log` 会忽略所有日志文件,但如果后面有 `!important.log`,则 `important.log` 将不会被忽略。

4. **忽略规则的精确匹配**:

- 如果规则没有以 `/` 结尾,它将被当作一个模式匹配。例如,`temp/` 只会忽略 `temp` 目录下的文件,而不会忽略 `tempfile`。

5. **忽略规则的相对路径问题**:

- `.gitignore` 文件中的规则是相对于仓库根目录的,如果需要忽略特定子目录下的文件,需要使用相对路径。

6. **使用全局 `.gitignore`**:

- Git 允许设置全局 `.gitignore` 文件,但使用时需要注意,这会影响到所有本地仓库。可能不适合所有项目。

7. **忽略规则的复杂性**:

- 过度复杂的 `.gitignore` 文件可能会难以维护和理解。尽量保持规则简单明了。

8. **忽略操作系统生成的文件**:

- 有些文件是操作系统生成的,如 `.DS_Store`(macOS),这些文件应该被忽略,但需要确保在 `.gitignore` 中正确指定。

9. **忽略临时文件**:

- 临时文件(如 `*.tmp` 或 `*.swp`)也应该被忽略,以避免将临时生成的文件加入版本控制。

10. **忽略构建产物**:

- 构建产物(如 `node_modules`、`dist/` 等)应该被忽略,因为它们可以通过构建脚本重新生成。

11. **忽略敏感信息**:

- 确保不会忽略掉包含敏感信息的文件,如 `.env` 文件或包含 API 密钥的配置文件。

12. **忽略规则的测试**:

- 在提交之前,使用 `git status` 或 `git check-ignore <file>` 来测试 `.gitignore` 文件的规则是否按预期工作。

通过避免这些常见的错误和误解,可以更有效地使用 `.gitignore` 文件来管理 Git 仓库中的文件。

【小结】

别小看一个个运维的小技术点,有时候能帮上大忙。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档