1

我在本地目录中存在我需要的未跟踪文件,但不希望提交到远程存储库(例如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 帖子.gitignore,他们的解决方案是使用文件。就我而言,它不起作用:每当我使用git add .或添加git add -A 所有文件时,无论它们是否存在于.gitignore.

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

如何绕过这个问题?

4

3 回答 3

7

用这个 :

git add --all

或这个 :

git add -A
于 2020-02-24T12:39:15.430 回答
3

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

.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 已经总结了这些目录的内容,而不是列出单个文件。只能在具有实际文件名的情况下解释这一点,这需要git status使用-uor--untracked-files=标志运行,并给这个标志一个词all来告诉 Git 单独列出每个未跟踪的文件。

没有这个标志......好吧,比如说,它Tests包含:

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

这三个文件中的一个与*.obj模式匹配,但另外两个与模式不匹配,因此 Git 会抱怨Tests/a.b并且Tests/c.d未被跟踪。但是如果没有-uall,Git 会抱怨这个的方式就是说它Tests/未跟踪的。即使其中的每个文件都被标记为“不要Tests/抱怨”也是如此该 Git 中的文件尚未被告知“关闭此文件”。TestsTestsTests/ Tests/Tests/

有一个非常神秘,那个是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的文件已经在索引中时,就是这样。

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

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

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

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

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

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

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

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

现在,如果 (say) 中有一些被跟踪的文件packages,Gitpackages无论如何都必须进入,将这些文件的索引副本与其工作树副本进行比较。但是我们在这里假设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 -vDebug/

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

于 2018-10-14T19:19:34.217 回答
0

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

于 2021-03-21T09:55:26.837 回答