简体   繁体   English

无法将丢失的文件添加到 VS2015 VSO Git

[英]Cannot add missing files to VS2015 VSO Git

I have added some public lib files manually (ie not via Bower) to a MEAN project.我已经手动(即不是通过 Bower)向 MEAN 项目添加了一些公共库文件。 My local Git repo is happy that they are new (see attached image).我的本地 Git 存储库很高兴它们是新的(见附图)。 However, when I try to either 'Commit' or 'Commit and Push' I get the error message 'No changes;但是,当我尝试“提交”或“提交并推送”时,我收到错误消息“无更改; nothing to commit':没有什么可提交的':

在此处输入图片说明

What's going on?这是怎么回事? How can I force these new files into the shared repo so that other devs can pull them?如何强制这些新文件进入共享存储库,以便其他开发人员可以提取它们?

Check your .gitignore file.检查您的.gitignore文件。

Visual Studio sometimes gets confused about what to show in the Included Changes section, because it's not just relying on what git tells it. Visual Studio 有时会对“包含的更改”部分中显示的内容感到困惑,因为它不仅仅依赖于 git 告诉它的内容。 I believe it monitors the filesystem for changes (but see note below)), and I think it also may hook into the events Visual Studio raises to tell source control providers about changes (which, way back, used to be the only kind of change source control providers would ever notice).我相信它会监视文件系统的更改(但请参阅下面的注释)),并且我认为它也可能与 Visual Studio 引发的事件挂钩,以告诉源代码控制提供程序有关更改的信息(回过头来,这曾经是唯一一种更改源代码控制提供者会注意到)。

This can cause various problems.这会导致各种问题。 One is that VS may fail to realised that git is going to decide that a file doesn't really need to be handled - eg, either because it doesn't look to git like it's different from the previous version (and line endings can complicate matters here), or apparently, as in this case, because it's in the .gitignore .一个是 VS 可能没有意识到 git 将决定一个文件并不真正需要处理 - 例如,要么因为它看起来不像 git 与以前的版本不同(并且行尾可能会复杂化)在这里很重要),或者显然,在这种情况下,因为它在.gitignore Normally VS does honour the .gitignore , but there are scenarios in which it doesn't.通常 VS 会尊重.gitignore ,但在某些情况下它不会。 Eg, I think if VS saw the file being added at a time when it wasn't listed in .gitignore , it can then stay in the list after you've told git to ignore it.例如,我认为如果 VS 在文件未在.gitignore列出的时候看到它被添加,那么在你告诉 git 忽略它之后它可以留在列表中。

There are more subtle problems that can arise too - when you move or rename files, Visual Studio can form a different opinion from git on what really happened.也可能出现更微妙的问题 - 当您移动或重命名文件时,Visual Studio 可以对实际发生的事情形成与 git 不同的意见。 (Eg, the VS git provider might have been notified by VS that a particular file was renamed, but git doesn't have that history, and if the file has changed enough it might decide that it's actually a new file.) (例如,VS git 提供程序可能已被 VS 通知某个特定文件已重命名,但 git 没有该历史记录,并且如果文件更改得足够多,它可能会决定它实际上是一个新文件。)

As a general rule, run git status at the command line to see what git really thinks.作为一般规则,在命令行运行git status以查看 git 的真实想法。 That will report the definitive truth - if git doesn't list something here as having changed, then it doesn't matter what VS reports.这将报告确定的事实 - 如果 git 没有在此处列出已更改的内容,那么 VS 报告的内容无关紧要。

NOTE: (added in edit 2015/11/30).注意:(在 2015/11/30 编辑中添加)。 In the comments, Edward Thomson says that "VS doesn't simply throw paths into a list based on filesystem activity, it recomputes the git status."在评论中,Edward Thomson 说“VS 不会简单地根据文件系统活动将路径放入列表中,它会重新计算 git 状态。” So perhaps my explanation for why VS sometimes shows confusing things is wrong.所以也许我对为什么 VS 有时会显示令人困惑的事情的解释是错误的。 Then again, it can't just be (as Edward put it) "recomput[ing] the git status" because VS very often comes up with a different answer from git.再说一次,它不能只是(正如爱德华所说的)“重新计算 [ing] git 状态”,因为 VS 经常提出与 git 不同的答案。 (That, after all, is why Chris posted his question in the first place.) (毕竟,这就是克里斯首先发布他的问题的原因。)

Also, Edward Thomson says, "You must have a .gitignore in place before creating the file (like with git add.)" But for at least one reasonable definition of "creating the file", this is not so.此外,Edward Thomson 说,“在创建文件之前,您必须有一个 .gitignore 文件(如使用 git add。)”但对于“创建文件”的至少一个合理定义来说,情况并非如此。 Eg:例如:

PS C:\temp\gittest> git init
Initialized empty Git repository in C:/temp/gittest/.git/
PS C:\temp\gittest> notepad Test.foo

If type in some text and save the file in notepad, then I have "created the file" at this point.如果输入一些文本并将文件保存在记事本中,那么此时我已经“创建了文件”。 Here's how git sees it:这是 git 的看法:

PS C:\temp\gittest> git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        Test.foo

nothing added to commit but untracked files present (use "git add" to track)

Created, but not added.已创建,但未添加。 (Two different things, as far as git is concerned.) Now I'm going to go on and create a .gitignore: (就 git 而言,这是两个不同的事情。)现在我将继续创建一个 .gitignore:

PS C:\temp\gittest> notepad .gitignore

I type in *.foo and save the file, and then continue at the command line, starting by verifying that my .gitignore contains what I think it does:我输入*.foo并保存文件,然后在命令行继续,首先验证我的.gitignore包含我认为的内容:

PS C:\temp\gittest> type .\.gitignore
*.foo
PS C:\temp\gittest> git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gitignore

nothing added to commit but untracked files present (use "git add" to track)
PS C:\temp\gittest>

As you can see I did not initially have a .gitignore in place, so the first time I ran git status it reported my Test.foo file as being Untracked (so it would get added if I did a git add . ) but then I added a .gitignore later and when I ran git status again it had stopped showing my Test.foo because my newly added .gitignore told it to.正如你所看到的,我最初没有.gitignore ,所以当我第一次运行git status它报告我的Test.foo文件未跟踪(所以如果我做了一个git add . ,它会被git add . )但后来我后来添加了.gitignore ,当我再次运行git status时,它停止显示我的Test.foo因为我新添加的.gitignore告诉它。 Thus we see that contrary to Edward's statement, it was absolutely fine to create the .gitignore after creating the file.因此,我们看到与 Edward 的声明相反,创建文件创建.gitignore绝对没问题。

I think the heart of this is that Edward conflates creating the file with adding it to git's index.我认为这的核心是 Edward 将创建文件与将其添加到 git 的索引混为一谈。 He wrote the phrase "creating the file (like with git add.)" but those aren't the same - creating the file is a distinct step from using git add .他写了“创建文件(就像使用 git add.)”这样的短语,但这些并不相同——创建文件与使用git add And for me (and for a lot of the people I know who learned who learned git on the command line, and have subsequently struggled slightly with VS) this is exactly why VS's git integration can sometimes be so surprising.而对我(以及我认识的很多人来说,他们是在命令行上学习 git 的,后来在 VS 上略有挣扎)这正是为什么 VS 的 git 集成有时会如此令人惊讶的原因。 Using git directly, creating a file is a very different, separate step from telling git to do something with that file.直接使用 git,创建文件是一个非常不同的步骤,与告诉 git 对该文件执行某些操作不同。 Git shows newly created files as 'Untracked', and only starts paying proper attention to them if you git add them. Git 将新创建的文件显示为“未跟踪”,并且只有在您git add时才开始适当注意它们。 (And actually committing the file is yet another separate step again of course.) (当然,实际上提交文件又是另一个单独的步骤。)

What I found very confusing at first is that VS's git support looks like it's making the same distinction: you have "Untracked Files", "Included Changes" and actual commits.起初我发现非常令人困惑的是,VS 的 git 支持看起来也有同样的区别:您有“未跟踪的文件”、“包含的更改”和实际提交。 It's not immediately obvious that VS's notion of an "Untracked File" is not at all the same as git's - everything VS shows in "Included Changes" still shows up as "Untracked files" in git (or, in Chris's case, doesn't show up at all because of the .gitignore .) In git, you move files from the "Untracked" to the "In git's purview but not yet committed" state with git add . VS 的“未跟踪文件”的概念与 git 的完全不同,这一点并不是很明显——VS 在“包含的更改”中显示的所有内容在 git 中仍然显示为“未跟踪的文件”(或者,在 Chris 的情况下,没有由于.gitignore 。)在 git 中,您使用git add将文件从“未跟踪”移动到“在 git 的权限范围内但尚未提交”状态。 Conversely, the mechanism by which VS decides whether files should be in "Included Changes" vs "Untracked Files" is a complete mystery to me.相反,VS 决定文件是否应该在“包含的更改”与“未跟踪的文件”中的机制对我来说是一个完整的谜。 If you add new files to a project, they mostly end up in "Included Changes" but occasionally end up in "Untracked Files" for no obvious reason.如果您将新文件添加到项目中,它们通常会以“包含的更改”结束,但偶尔也会无缘无故地以“未跟踪的文件”结束。

The thing that makes "Included Changes" particularly baffling for git command line users is that VS appears to apply rules for .gitignore to those files in the same way as git would if you had already added the files, but the files have not in fact been added.让“包含的更改”对 git 命令行用户特别困惑的事情是,VS 似乎将.gitignore规则应用于这些文件,就像 git 如果您已经添加了文件一样,但文件实际上并没有已添加。 And this is exactly what leads to the situation Chris encountered in his question: VS thinks there are changes to commit (because it's processing the Included Changes as though git add had already been done for those) but there aren't (because as far as git is concerned, these are all Untracked files, and so they are all subject to .gitignore ).这正是导致 Chris 在他的问题中遇到的情况的原因:VS 认为有更改要提交(因为它正在处理包含的更改,就好像git add已经为这些更改完成了一样)但没有(因为就这些更改而言) git 而言,这些都是未跟踪的文件,因此它们都受.gitignore约束)。

This is what lead me to think tha VS must be monitoring the filesystem for changes - in the absence of a direct equivalent to the explicit git add operations we perform at the command line, I don't know how it could even know whether a particular file was created before or after .gitignore if it wasn't watching the filesystem.这让我认为 VS 必须监视文件系统的更改 - 在没有直接等效于我们在命令行执行的显式git add操作的情况下,我不知道它如何甚至知道特定的如果文件不是在监视文件系统,则文件是在.gitignore之前或之后创建的。

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

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