简体   繁体   English

如何修复 gitignore 中的 vscode 跟踪

[英]How to fix .vscode tracking in gitignore

I'm having trouble using .gitignore with my .vscode folder in my repository.我在将.gitignore与存储库中的.vscode文件夹一起使用时遇到问题。

I added a rule in my .gitignore to ignore my entire .vscode/ folder:我在我的.gitignore中添加了一条规则来忽略我的整个.vscode/文件夹:

# Visual Studio Code # 
.vscode/* 
.vscode/settings.json
!.vscode/settings.json 
!.vscode/tasks.json 
!.vscode/launch.json 
!.vscode/extensions.json 
.history.vscode/settings.json

Despite this, .vscode is still being tracked by my local git repository.尽管如此,我的本地.vscode存储库仍在跟踪 .vscode。 I've tried a variety of different solutions including using git filter-branch to remove this folder from my git history and git rm --cached.vscode to clear my cache and re-commit my changes, but none of these solutions prevents the.vscode folder from being tracked by git.我尝试了多种不同的解决方案,包括使用git filter-branch从我的 git 历史记录中删除此文件夹,以及git rm --cached.vscode清除我的缓存并重新提交我的更改,但这些解决方案都无法阻止。 vscode 文件夹被 git 跟踪。

When I run git status I keep getting this result:当我运行git status时,我不断得到这个结果:

On branch master
Your branch is up to date with 'origin/master'.

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

    .vscode/

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

So far, I've worked around this by manually excluding this folder when I git add my changes.到目前为止,我已经通过在git add我的更改时手动排除此文件夹来解决此问题。

Why is .vscode/ still being shown as untracked?为什么.vscode/仍然显示为未跟踪? If I were to git add.如果我要git add. and git commit this, it would add this folder to my git history and eventually to my remote repository when I git push .git commit这个,它会将这个文件夹添加到我的 git 历史记录中,并最终在我git push时添加到我的远程存储库中。 I don't want this behavior to occur.我不希望发生这种行为。

Had similar problem, turned out the files were added to my cache.有类似的问题,原来文件被添加到我的缓存中。 Clearing it with below command worked.使用以下命令清除它有效。

git rm --cached .vscode/settings.json

Reference to the issue on github where I found the solution.参照问题上github上,我找到了解决办法。

Maybe try this in your .gitignore.也许在你的 .gitignore 中试试这个。 This should ignore the entire .vscode directory, no matter where it is located.这应该忽略整个 .vscode 目录,无论它位于何处。

**/.vscode/

Happened for an Untracked git file .vscode/settings.json .发生在未跟踪的 git 文件.vscode/settings.json Turned out to be a second line in our huge .gitignore file that was overriding the effort.结果是我们巨大的.gitignore文件中的第二行覆盖了工作。 In example below simply remove the bottom line.在下面的示例中,只需删除底线。

Remember to re-stage and commit .gitgnore after the change.请记住在更改后重新.gitgnore并提交.gitgnore

# IDEs and editors
.settings/
.vscode/settings.json   -- Line I added that "was not working"
...

# IDE - VSCode
.vscode/*
!.vscode/settings.json  -- Line discovered that was overriding above (REMOVE)
...

As there can be three types of files(tracked, untracked, and ignored).因为可以有三种类型的文件(跟踪、未跟踪和忽略)。 The files which are already tracked or indexed can't be ignored just by adding them to the.gitignore.已经被跟踪或索引的文件不能仅仅通过将它们添加到 the.gitignore 来忽略。 Rather their index should be deleted.相反,他们的索引应该被删除。 Then if you add them in the.gitignore it will be ignored.然后,如果您将它们添加到 the.gitignore 中,它将被忽略。

Suppose you have a tracked file in the root named test.txt now you are adding this file to the.gitignore.假设您在根目录中有一个名为test.txt的跟踪文件,现在您正在将此文件添加到 the.gitignore。 It will not work unless you remove it from the git index like below它不会工作,除非你将它从 git 索引中删除,如下所示

 git rm --cached test.txt

Also, you can use the relative path of a file to remove it if they are not in the root.此外,如果文件不在根目录中,您可以使用文件的相对路径将其删除。

Then the index will be deleted and git will stage them as files deleted but they won't be deleted from your local.然后索引将被删除,git 会将它们暂存为已删除的文件,但它们不会从您的本地删除。

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

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