简体   繁体   English

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

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

I have untracked files present that I need in the local directory, but wish not to commit to the remote repo (eg obj files, mtl files, .vs folder and so on).我在本地目录中有我需要的未跟踪文件,但不希望提交到远程存储库(例如obj文件、 mtl文件、 .vs文件夹等)。 I have this .gitignore file I use:我有我使用的这个.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/

And as you see, it is clearly stated that I want git to ignore the files tracked.正如你所看到的,它明确指出我希望 git 忽略跟踪的文件。 Yet I get this message:但我收到了这条消息:

git status On branch default_shader Untracked files: (use "git add ..." to include in what will be committed) git status 在分支 default_shader 未跟踪的文件中:(使用“git add ...”来包含将提交的内容)

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

I followed this SO post on the exact same problem and their solution was to use a .gitignore file.我在完全相同的问题上关注了这篇 SO post ,他们的解决方案是使用.gitignore文件。 In my case it didn't work: whenever I use git add .就我而言,它不起作用:每当我使用git add . or git add -A all files get added, regardless of their presence in .gitignore .git add -A所有文件都会被添加,无论它们是否存在于.gitignore

Note I get this message I quoted after git rm -r --cached .注意我收到了在git rm -r --cached .之后引用的这条消息git rm -r --cached . to unstage my previously added unwanted files.取消暂存我之前添加的不需要的文件。

How to get around the issue?如何解决这个问题?

Use this :用这个 :

git add --all

Or this :或这个 :

git add -A

The list of untracked files reads (this is just a straight quote of your own quote):未跟踪文件列表读取(这只是您自己引用的直接引用):

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

Now, note that there are only three names here that do not end with a slash:现在,请注意这里只有三个名称以斜线结尾:

  1. .gitignore

    You probably should add and commit this file, so you probably do want it to be untracked at the moment, until you add and commit it.你或许应该添加并提交该文件,所以你可能希望它是未经跟踪的那一刻,直到您添加并提交。

  2. 3dengine_headeronly.sln

    This file is not listed as "do not auto-add nor complain about untracked-ness".此文件未列为“不要自动添加或抱怨未跟踪”。 Check it out yourself: search through your .gitignore contents for anything resmbling 3dengine_headeronly.sln .自己检查一下:在您的.gitignore内容中搜索任何类似于3dengine_headeronly.sln内容。 One entry that comes close is the last line in this group:接近的一个条目是该组中的最后一行:

     # JetBrains Rider .idea/ *.sln.iml

    which lists *.sln.iml as "do not auto-add nor complain".*.sln.iml列为“不要自动添加或抱怨”。 But 3dengine_headeronly.sln ends with .sln , not .sln.iml .但是3dengine_headeronly.sln.sln结尾,而不是.sln.iml Another is the line *.sln.docstates , but again, that ends with .sln.docstates , and the file's actual name ends with just .sln .另一个是*.sln.docstates行,但同样以.sln.docstates ,文件的实际名称仅以.sln结尾。

  3. CMakeLists.txt

    As before, there is no entry that would make Git shut up about this.和以前一样,没有任何条目可以让 Git 对此闭嘴。

That leaves unexplained the entries ending with slashes.这使得以斜杠结尾的条目无法解释。 The problem with diagnosing this is that Git has summarized the contents of those directories, rather than listing the individual files.诊断的问题在于 Git 汇总了这些目录的内容,而不是列出单个文件。 One can only explain this if one has the actual file names, which requires running git status with the -u or --untracked-files= flag, and giving this flag the word all to tell Git to list each untracked file individually.只有当有实际的文件名时才能解释这一点,这需要使用-u--untracked-files=标志运行git status ,并给这个标志一个单词all来告诉 Git 单独列出每个未跟踪的文件。

Without this flag ... well, let's say, for instance, that Tests contains:没有这个标志......好吧,让我们说,例如, Tests包含:

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

One of these three files matches the *.obj pattern, but the other two match no pattern, so Git will complain that Tests/ab and Tests/cd are untracked.这三个文件中的一个匹配*.obj模式,但其他两个不匹配任何模式,因此 Git 会抱怨Tests/abTests/cd未被跟踪。 But without -uall , the way Git will complain about this is to say that Tests/ is untracked.但是,如果没有-uall ,Git会抱怨这样的方式是说, Tests/是未经跟踪。 This is true even if every file within Tests/ is marked as "don't complain" except for one file in Tests or some subdirectory within Tests : Git will summarize the one file by printing Tests/ because there are no tracked files within Tests/ , but there is at least one untracked file within Tests/ that Git has not been told "shut up about this file".这是真实的,即使内的每一个文件Tests/被标记为“不抱怨”除了在一个文件Tests或内的一些子目录Tests :Git会通过打印总结了一个文件, Tests/因为有没有跟踪文件Tests/ ,但在Tests/中至少有一个未跟踪的文件,Git没有被告知“关闭这个文件”。

There is one that is pretty mysterious, and that one is Debug/ .有一种非常神秘,一种是Debug/ You do have this line in your .gitignore :你的.gitignore有这一行:

 [Dd]ebug/

This should cause all files within Debug and any subdirectory inside Debug to be not-complained-about.这应当引起内的所有文件Debug和任何子目录中Debug是不是-抱怨-约。 Using -uall will probably shed some light on what is going on, though.不过,使用-uall可能会阐明正在发生的事情。 (See below for why this one is such a mystery.) (有关为什么这个如此神秘,请参见下文。)

Long: how git status scans the work-tree Long: git status如何扫描工作树

It's important to realize that Git never tracks a directory itself.认识到 Git 从不跟踪目录本身很重要。 What listing a directory in .gitignore does is give Git permission not to examine the directory..gitignore中列出目录的作用是授予 Git 不检查目录的权限。

To understand this we need to define what it means to track a file in the first place, and how Git examines your work-tree when it wants to collect up a list of untracked files for git status to complain about.为了理解这一点,我们首先需要定义跟踪文件的含义,以及当 Git 想要收集未跟踪文件列表以供git status抱怨时,它如何检查您的工作树。

A tracked file, in Git, is simply any file whose name is currently listed in Git's index .在 Git 中,跟踪文件只是名称当前列在 Git 索引中的任何文件 To see which files are currently in the index, you can run git ls-files --stage (but note that in a big directory this tends to list a lot of files).要查看索引中当前有哪些文件,您可以运行git ls-files --stage (但请注意,在大目录中,这往往会列出很多文件)。 These are the files that will be in the next commit you make.这些是将在您进行的下一次提交中的文件。 Initially, the index is full of all the files that were in the commit you most recently checked out.最初,索引充满了您最近检出的提交中的所有文件。 The next commit will contain all those same files.下一次提交将包含所有这些相同的文件。 If you overwrite one of the index copies, the next commit will contain the updated index copy—and that's what git add does, when you git add a file that's already in the index.如果您覆盖其中一个索引副本,下一次提交将包含更新的索引副本——这就是git add所做的,当您git add一个已经在索引中的文件时。

Hence, an untracked file is simply any file that exists in the work-tree, but that is not currently in the index .因此,未跟踪文件只是工作树中存在但当前不在 index 中的任何文件 Using git rm —with or without --cached —you can remove any file from the index, if it's currently in the index.使用git rm — 有或没有--cached — 你可以从索引中删除任何文件,如果它当前在索引中。 Once it's gone from the index, if you've kept the work-tree copy, it has become untracked.一旦它从索引中消失,如果您保留了工作树副本,它就无法被跟踪。

What git status does is to run two comparisons—two git diff s, in effect. git status所做的是运行两个比较——实际上是两个git diff The first one compares the current commit frozen files to the index's unfrozen (but Git-ified) files: whatever is the same, Git says nothing about, and whatever is different, Git says: file with pathname P is staged for commit .第一个将当前提交冻结的文件与索引的未冻结(但 Git 化)文件进行比较:无论是相同的,Git 什么都不说,无论是什么不同的,Git 都会说:具有路径名 P 的文件被暂存为提交 This is way better than listing every file in the index: it tells you about file P only if it's going to be different in the next commit.这比列出索引中的每个文件要好得多:它仅在文件P在下一次提交中有所不同时才告诉您有关文件P的信息。

The second comparison is the tricky one, and is where .gitignore comes in. To run the second comparison, Git compares the Git-ified but not-yet-frozen copy of a file that's in the index with the regular, non-Git-ified copy in the work-tree.第二个比较是棘手的,这是.gitignore用武之地。为了运行第二个比较,Git 将索引中的文件的 Git 化但尚未冻结的副本与常规的非 Git 进行比较。工作树中的化副本。 When the two are different, Git tells you that this file is not staged for commit .当两者不同时,Git 会告诉你这个文件没有为 commit 暂存 You can run git add to copy the work-tree copy over top of the index copy, so that the index and work-tree match—and now, presumably, the HEAD commit and index copies of the file differ, so that the next git status will say staged for commit .您可以运行git add将工作树副本复制到索引副本的顶部,以便索引和工作树匹配 - 现在,据推测,文件的HEAD提交和索引副本不同,以便下一个git status会说staged for commit

That works great for files that are in both the index and the work-tree, but fails to alert you to files that you forgot to git add to copy into the index.这对于同时位于索引和工作树中的文件非常有用,但无法提醒您忘记git add复制索引中的文件。 So, having collected up the list of comparison results, Git also has to scan through the work-tree to find every file that's actually there .因此,收集比较结果列表后,Git必须扫描工作树以找到实际存在的每个文件。 Any file that is in the work-tree, but does not have a corresponding copy in the index, is untracked .任何在工作树中但在索引中没有相应副本的文件都不会被跟踪

This scanning is one of the slower parts of git status (though the actual speed depends a lot on the speed of your operating system).此扫描是git status较慢的部分之一(尽管实际速度在很大程度上取决于操作系统的速度)。 Git starts by reading through every file and sub-directory ("folder") name within the top level work-tree directory. Git 首先通读顶级工作树目录中的每个文件和子目录(“文件夹”)名称。 It has to read this directory entirely, of course, because it's the top level of the work-tree.当然,它必须完全读取这个目录,因为它是工作树的顶层。 Any file name in this directory represents a file in the work-tree, and the file is either tracked—in the index—or not.此目录中的任何文件名都代表工作树中的一个文件,并且该文件要么在索引中被跟踪,要么不被跟踪。 If the file is untracked , Git will complain about it unless you also suppress the complaint, using a .gitignore entry.如果文件未跟踪,Git 会抱怨它,除非你使用.gitignore条目抑制抱怨。

For directories found in the top level, though, git status does not check whether the directory is in the index, because directories are never in the index.但是,对于在顶层找到的目录git status不会检查目录是否在索引中,因为目录从不在索引中。 Instead, Git just looks inside the directory to see what files and directories it contains.相反,Git 只是查看目录内部以查看它包含哪些文件和目录。 Any files within that directory have to be checked to see if they're untracked, and if so, whether git status should gripe about them.必须检查该目录中的任何文件以查看它们是否未被跟踪,如果是,则git status是否应该抱怨它们。 If the directory contains its own sub-directories, those sub-directories must be scanned in the same way, recursively.如果目录包含自己的子目录,则必须以相同的方式递归扫描这些子目录。

What listing a directory in .gitignore does is give Git permission to not look inside it..gitignore中列出目录的作用是授予 Git查看其内部的权限。 Let's consider the Debug/ directory specifically, for instance.例如,让我们具体考虑Debug/目录。 Let's assume that there are no tracked files inside Debug/ , and that you have run git status .假设Debug/中没有跟踪文件,并且您已运行git status

Git will start by reading the top level directory. Git 将首先读取顶级目录。 In this directory, it finds files named .gitignore , 3dengine_headeronly.sln , and CMakeLists.txt .在此目录中,它会找到名为.gitignore3dengine_headeronly.slnCMakeLists.txt Those files are not in the index and not listed as a name or pattern in .gitignore , so git status will complain about them (at the end, when it lists untracked files).这些文件不在索引中,也没有.gitignore列为名称或模式,因此git status会抱怨它们(最后,当它列出未跟踪的文件时)。 But it also finds .vs/ , 3dengine_headeronly/ , Debug/ , Tests/ , cmake-build-debug/ , packages/ , and resources/ .但它也会找到.vs/3dengine_headeronly/Debug/Tests/cmake-build-debug/packages/resources/

Now, if there are some tracked files within (say) packages , Git's going to have to descend into packages anyway, to compare the index copies of those files with their work-tree copies.现在,如果(比如说) packages有一些被跟踪的文件,Git 无论如何都必须深入到packages ,以将这些文件的索引副本与其工作树副本进行比较。 But we've assumed here that there are no tracked files in Debug .但是我们在这里假设Debug中没有跟踪文件。 So git status should be able to run through the .gitignore entries and see that [Dd]ebug (Git removes the trailing slash for comparison purposes) matches Debug .所以git status应该能够遍历.gitignore条目并看到[Dd]ebug (Git 删除尾部斜杠以进行比较)匹配Debug This would permit git status to skip over Debug entirely, not reading it at all.这将允许git status完全跳过Debug ,根本不读取它。

The fact that Debug/ comes out at the end, with just the trailing slash, means that Git must have opened and scanned the Debug directory.事实上Debug/出现在最后,只有尾部斜杠,这意味着 Git必须已经打开并扫描了Debug目录。 The only reason Git would do that is if there is at least one tracked file inside Debug . Git 这样做的唯一原因是如果Debug中至少有一个被跟踪的文件。 Hence:因此:

git ls-files --stage | grep Debug/

will probably show at least one tracked file.可能会显示至少一个跟踪文件。 Even so, it's not clear why the [Dd]ebug/ rule did not match the untracked files contained in the directory, so getting a list of the exact names, and using git --no-index check-ignore -v on any tracked files inside Debug/ , might be helpful.即便如此,仍不清楚为什么[Dd]ebug/规则与目录中包含的未跟踪文件不匹配,因此获取确切名称的列表,并在任何被跟踪的文件上使用git --no-index check-ignore -v Debug/文件可能会有所帮助。

(This optimization, where git status sometimes doesn't open a directory at all, is especially tricky, and is where a lot of .gitignore issues start. There's not much to be done about this until and unless someone teaches Git better ways to skip, or not skip, entire directories. However, using git status -uall definitely helps some of the other tricky cases.) (这种优化,其中git status有时根本不打开目录,特别棘手,并且是许多.gitignore问题开始的地方。在此之前没有太多.gitignore ,除非有人教 Git 更好的跳过方法,或者不跳过,整个目录。但是,使用git status -uall肯定有助于其他一些棘手的情况。)

In my case it was only a bug.就我而言,这只是一个错误。 When I pushed to github I have seen that commit isn't actually empty.当我推送到 github 时,我看到提交实际上不是空的。 Make sure to add all modified files to commit.确保将所有修改过的文件添加到提交中。

暂无
暂无

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

相关问题 Git:没有添加任何提交但存在未跟踪的文件 - Git:nothing added to commit but untracked files present npm-debug.log位于.gitignore文件中,但我仍然收到消息“没有添加任何内容来提交但存在未跟踪的文件(使用” git add”进行跟踪)” - npm-debug.log is in .gitignore file, but I still get the messsage “nothing added to commit but untracked files present (use ”git add“ to track)” Git-列出根目录中的每个文件后,“没有添加任何内容来提交但存在未跟踪的文件” - Git - “nothing added to commit but untracked files present” after listing every file in root Git 重新索引远程存储库中未跟踪的添加文件 - Git reindex untracked added files in remote repository 冻结的文件被添加到提交中,忽略 gitignore - Freezed files are added to the commit, ignoring gitignore git 中未跟踪的文件不是.gitignore 的一部分 - Files untracked in git not part of .gitignore Git commit -a “未跟踪的文件”? - Git commit -a “untracked files”? 添加到 Git 子模块中的 .gitignore 文件 - .gitignore files added inside Git submodules git 显示已添加到 gitignore 的文件中的更改? - git is showing changes in files that are already added to gitignore? git checkout HEAD〜2(示例)将删除未跟踪的文件(如果它们是在上次提交时添加的) - git checkout HEAD~2 (example) deletes untracked files if they were added with last commit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM