繁体   English   中英

Git 没有向提交添加任何内容,但存在未跟踪的文件 - 即使将这些文件添加到 gitignore

[英]Git nothing added to commit but untracked files present - even with those files added to gitignore

我在本地目录中有我需要的未跟踪文件,但不希望提交到远程存储库(例如obj文件、 mtl文件、 .vs文件夹等)。 我有我使用的这个.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
*.suo
*.user
*.userosscache
*.sln.docstates
*.obj
*.png
*.log
*.blend
*.blend1
*.mtl
*.jpg
*.jpeg
*.opendb


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

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/

# 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

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

# 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
*.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

# JustCode is a .NET coding add-in
.JustCode

# TeamCity is a build add-in
_TeamCity*

# DotCover is a Code Coverage Tool
*.dotCover

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

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

# 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

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

# JetBrains Rider
.idea/
*.sln.iml

# CodeRush
.cr/

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

正如你所看到的,它明确指出我希望 git 忽略跟踪的文件。 但我收到了这条消息:

git status 在分支 default_shader 未跟踪的文件中:(使用“git add ...”来包含将提交的内容)

 .gitignore .vs/ 3dengine_headeronly.sln 3dengine_headeronly/ CMakeLists.txt Debug/ Tests/ cmake-build-debug/ packages/ resources/

我在完全相同的问题上关注了这篇 SO post ,他们的解决方案是使用.gitignore文件。 就我而言,它不起作用:每当我使用git add . git add -A所有文件都会被添加,无论它们是否存在于.gitignore

注意我收到了在git rm -r --cached .之后引用的这条消息git rm -r --cached . 取消暂存我之前添加的不需要的文件。

如何解决这个问题?

用这个 :

git add --all

或这个 :

git add -A

未跟踪文件列表读取(这只是您自己引用的直接引用):

 .gitignore .vs/ 3dengine_headeronly.sln 3dengine_headeronly/ CMakeLists.txt Debug/ Tests/ cmake-build-debug/ packages/ resources/

现在,请注意这里只有三个名称以斜线结尾:

  1. .gitignore

    你或许应该添加并提交该文件,所以你可能希望它是未经跟踪的那一刻,直到您添加并提交。

  2. 3dengine_headeronly.sln

    此文件未列为“不要自动添加或抱怨未跟踪”。 自己检查一下:在您的.gitignore内容中搜索任何类似于3dengine_headeronly.sln内容。 接近的一个条目是该组中的最后一行:

     # JetBrains Rider .idea/ *.sln.iml

    *.sln.iml列为“不要自动添加或抱怨”。 但是3dengine_headeronly.sln.sln结尾,而不是.sln.iml 另一个是*.sln.docstates行,但同样以.sln.docstates ,文件的实际名称仅以.sln结尾。

  3. CMakeLists.txt

    和以前一样,没有任何条目可以让 Git 对此闭嘴。

这使得以斜杠结尾的条目无法解释。 诊断的问题在于 Git 汇总了这些目录的内容,而不是列出单个文件。 只有当有实际的文件名时才能解释这一点,这需要使用-u--untracked-files=标志运行git status ,并给这个标志一个单词all来告诉 Git 单独列出每个未跟踪的文件。

没有这个标志......好吧,让我们说,例如, Tests包含:

Tests/a.b
Tests/c.d
Tests/e.obj

这三个文件中的一个匹配*.obj模式,但其他两个不匹配任何模式,因此 Git 会抱怨Tests/abTests/cd未被跟踪。 但是,如果没有-uall ,Git会抱怨这样的方式是说, Tests/是未经跟踪。 这是真实的,即使内的每一个文件Tests/被标记为“不抱怨”除了在一个文件Tests或内的一些子目录Tests :Git会通过打印总结了一个文件, Tests/因为有没有跟踪文件Tests/ ,但在Tests/中至少有一个未跟踪的文件,Git没有被告知“关闭这个文件”。

有一种非常神秘,一种是Debug/ 你的.gitignore有这一行:

 [Dd]ebug/

这应当引起内的所有文件Debug和任何子目录中Debug是不是-抱怨-约。 不过,使用-uall可能会阐明正在发生的事情。 (有关为什么这个如此神秘,请参见下文。)

Long: git status如何扫描工作树

认识到 Git 从不跟踪目录本身很重要。 .gitignore中列出目录的作用是授予 Git 不检查目录的权限。

为了理解这一点,我们首先需要定义跟踪文件的含义,以及当 Git 想要收集未跟踪文件列表以供git status抱怨时,它如何检查您的工作树。

在 Git 中,跟踪文件只是名称当前列在 Git 索引中的任何文件 要查看索引中当前有哪些文件,您可以运行git ls-files --stage (但请注意,在大目录中,这往往会列出很多文件)。 这些是将在您进行的下一次提交中的文件。 最初,索引充满了您最近检出的提交中的所有文件。 下一次提交将包含所有这些相同的文件。 如果您覆盖其中一个索引副本,下一次提交将包含更新的索引副本——这就是git add所做的,当您git add一个已经在索引中的文件时。

因此,未跟踪文件只是工作树中存在但当前不在 index 中的任何文件 使用git rm — 有或没有--cached — 你可以从索引中删除任何文件,如果它当前在索引中。 一旦它从索引中消失,如果您保留了工作树副本,它就无法被跟踪。

git status所做的是运行两个比较——实际上是两个git diff 第一个将当前提交冻结的文件与索引的未冻结(但 Git 化)文件进行比较:无论是相同的,Git 什么都不说,无论是什么不同的,Git 都会说:具有路径名 P 的文件被暂存为提交 这比列出索引中的每个文件要好得多:它仅在文件P在下一次提交中有所不同时才告诉您有关文件P的信息。

第二个比较是棘手的,这是.gitignore用武之地。为了运行第二个比较,Git 将索引中的文件的 Git 化但尚未冻结的副本与常规的非 Git 进行比较。工作树中的化副本。 当两者不同时,Git 会告诉你这个文件没有为 commit 暂存 您可以运行git add将工作树副本复制到索引副本的顶部,以便索引和工作树匹配 - 现在,据推测,文件的HEAD提交和索引副本不同,以便下一个git status会说staged for commit

这对于同时位于索引和工作树中的文件非常有用,但无法提醒您忘记git add复制索引中的文件。 因此,收集比较结果列表后,Git必须扫描工作树以找到实际存在的每个文件。 任何在工作树中但在索引中没有相应副本的文件都不会被跟踪

此扫描是git status较慢的部分之一(尽管实际速度在很大程度上取决于操作系统的速度)。 Git 首先通读顶级工作树目录中的每个文件和子目录(“文件夹”)名称。 当然,它必须完全读取这个目录,因为它是工作树的顶层。 此目录中的任何文件名都代表工作树中的一个文件,并且该文件要么在索引中被跟踪,要么不被跟踪。 如果文件未跟踪,Git 会抱怨它,除非你使用.gitignore条目抑制抱怨。

但是,对于在顶层找到的目录git status不会检查目录是否在索引中,因为目录从不在索引中。 相反,Git 只是查看目录内部以查看它包含哪些文件和目录。 必须检查该目录中的任何文件以查看它们是否未被跟踪,如果是,则git status是否应该抱怨它们。 如果目录包含自己的子目录,则必须以相同的方式递归扫描这些子目录。

.gitignore中列出目录的作用是授予 Git查看其内部的权限。 例如,让我们具体考虑Debug/目录。 假设Debug/中没有跟踪文件,并且您已运行git status

Git 将首先读取顶级目录。 在此目录中,它会找到名为.gitignore3dengine_headeronly.slnCMakeLists.txt 这些文件不在索引中,也没有.gitignore列为名称或模式,因此git status会抱怨它们(最后,当它列出未跟踪的文件时)。 但它也会找到.vs/3dengine_headeronly/Debug/Tests/cmake-build-debug/packages/resources/

现在,如果(比如说) packages有一些被跟踪的文件,Git 无论如何都必须深入到packages ,以将这些文件的索引副本与其工作树副本进行比较。 但是我们在这里假设Debug中没有跟踪文件。 所以git status应该能够遍历.gitignore条目并看到[Dd]ebug (Git 删除尾部斜杠以进行比较)匹配Debug 这将允许git status完全跳过Debug ,根本不读取它。

事实上Debug/出现在最后,只有尾部斜杠,这意味着 Git必须已经打开并扫描了Debug目录。 Git 这样做的唯一原因是如果Debug中至少有一个被跟踪的文件。 因此:

git ls-files --stage | grep Debug/

可能会显示至少一个跟踪文件。 即便如此,仍不清楚为什么[Dd]ebug/规则与目录中包含的未跟踪文件不匹配,因此获取确切名称的列表,并在任何被跟踪的文件上使用git --no-index check-ignore -v Debug/文件可能会有所帮助。

(这种优化,其中git status有时根本不打开目录,特别棘手,并且是许多.gitignore问题开始的地方。在此之前没有太多.gitignore ,除非有人教 Git 更好的跳过方法,或者不跳过,整个目录。但是,使用git status -uall肯定有助于其他一些棘手的情况。)

就我而言,这只是一个错误。 当我推送到 github 时,我看到提交实际上不是空的。 确保将所有修改过的文件添加到提交中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM