簡體   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