简体   繁体   English

在 Visual Studio Code 中从 git 中排除文件夹

[英]Exclude folder from git in Visual Studio Code

I am using Visual Studio Code version 0.7.10.我正在使用 Visual Studio Code 版本 0.7.10。 I want to use version control.我想使用版本控制。 I figured this was a good time to start teaching myself to use git. I have never used it before.我认为这是开始自学使用 git 的好时机。我以前从未使用过它。

How do I make git ignore my "node_modules" folder?如何让 git 忽略我的“node_modules”文件夹? Every item in this folder is listed for possible commits and that is cluttering up my workspace.此文件夹中的每个项目都列出了可能的提交,这使我的工作空间变得杂乱无章。 I don't want to upload all the packages.我不想上传所有的包。 I figure whoever runs the package.json can do that on their own computer.我认为运行 package.json 的人都可以在自己的计算机上执行此操作。

Thought: I found under File > Preferences > User Settings that there is a configuration file called "settings.json" that will overwrite the default settings.想法:我在文件 > 首选项 > 用户设置下找到一个名为“settings.json”的配置文件,它将覆盖默认设置。 I see a "Git configuration" in the default settings but I can't find any documentation on my possible options here.我在默认设置中看到“Git 配置”,但我在这里找不到关于我的可能选项的任何文档。 I would think I could do a "git.exclude" but where do I begin.我想我可以做一个“git.exclude”,但我从哪里开始。 This is only a guess at a solution.这只是对解决方案的猜测。 I am sure the real answer is something completely different.我相信真正的答案是完全不同的。

I am looking for any documentation you could point me to.我正在寻找您可以指出的任何文档。 Thanks!谢谢!

UPDATE: I made a change to the ".git/info/exclude" file and my Git repository view cleared up instantly.更新:我对“.git/info/exclude”文件进行了更改,我的 Git 存储库视图立即被清除。 Here is a link that talks about the exclude file and why it worked for my VS Code editor.这是一个链接,讨论排除文件以及它为何适用于我的 VS 代码编辑器。 ( https://help.github.com/articles/ignoring-files/ ) ( https://help.github.com/articles/ignoring-files/ )

In git you can always use .gitignore to make git ignore some files.在 git 中,您始终可以使用.gitignore使 git 忽略某些文件。 You can find the documentation here : http://git-scm.com/docs/gitignore你可以在这里找到文档:http: //git-scm.com/docs/gitignore

You would make a .gitignore file in the base directory of your git repository and add any files or folders that you would like to ignore.您将在 git 存储库的基本目录中创建一个 .gitignore 文件,并添加您想忽略的任何文件或文件夹。 In your case your file would contain在您的情况下,您的文件将包含

/node_modules/

Documentation: http://git-scm.com/docs/gitignore文档:http: //git-scm.com/docs/gitignore

You can use this setting:您可以使用此设置:

"git.ignoredRepositories": []

which specifically does what you're asking.它专门满足您的要求。 This won't pollute your .gitignore file if the only thing you want to do is to exclude a folder from vscode git but not from git itself.如果您只想从 vscode git 中排除文件夹,而不是从 git 本身中排除文件夹,这不会污染您的 .gitignore 文件。 For example, this is useful if your home dir is under version control, since thousands of files would have to be ignored otherwise, which is hardly what you want.例如,如果您的主目录受版本控制,这很有用,因为否则必须忽略数千个文件,这几乎不是您想要的。

Follow the steps to add a folder to .gitignore using UI按照步骤使用 UI 将文件夹添加到 .gitignore

  1. Click Source Control on left pane单击左侧窗格上的源代码管理
  2. Click on three dots at right top of Source Control tab单击源代码管理选项卡右上角的三个点
  3. Select View & Sort>View as Tree选择查看和排序>查看为树
  4. Right click on the folder you want to add to .gitignore右键单击要添加到 .gitignore 的文件夹
  5. Select Add to .gitignore选择添加到 .gitignore

You are done!你完成了!

Github maintain a repository containing .gitignore files for a lot of languages. Github 维护一个包含多种语言的 .gitignore 文件的存储库。

Here is the one you need for Node... 是您需要的Node ...

Or you could generate one (very useful if you use different technologies in the same repository) using the web site: http://gitignore.io或者您可以使用以下网站生成一个(如果您在同一存储库中使用不同的技术,这将非常有用):http: //gitignore.io

Ps: Except if you've got really good reasons (exclude rules that you want to keep only for you), don't use .git/info/exclude to ignore files but prefer to use and commit the .gitignore file (to share it with all repository users). Ps:除非你有很好的理由(排除你只想为你保留的规则),不要使用.git/info/exclude来忽略文件,而是更喜欢使用和提交.gitignore文件(分享它与所有存储库用户)。

This is an old solved question, but for anyone else who is still struggling with the accepted (and correct) answer, you literally need to add a file to your main (ie root) project folder called:这是一个已解决的老问题,但对于仍在为接受(和正确)答案而苦苦挣扎的其他人来说,您实际上需要将一个文件添加到您的主(即根)项目文件夹中,名为:

.gitignore .gitignore

If you add helloworld.gitignore it won't work.如果你添加 helloworld.gitignore 它将不起作用。 Or whywontthiswork.gitignore, etc. It must be .gitignore.或者whywontthiswork.gitignore等,一定是.gitignore。

Inside the file, type this text:在文件中,键入以下文本:

/node_modules /node_modules

Assuming your placement of the .gitignore file is at the same relative path of the node_modules folder, anything in node_modules will be excluded from change tracking.假设您放置 .gitignore 文件的位置与 node_modules 文件夹的相对路径相同,那么 node_modules 中的任何内容都将被排除在更改跟踪之外。

There is a simple way to do this.有一个简单的方法可以做到这一点。

git rm <file-name>
git commit -m '<message>'
git push

for folders,对于文件夹,

git rm -r <folder-name>/*
git commit -m '<message>'
git push

Create a file with .gitignore & put the folder or file name which one you want to ignore.使用.gitignore创建一个文件并输入您要忽略的文件夹或文件名。

to ignore everything below node_modules folder忽略 node_modules 文件夹下的所有内容

echo node_modules/ > .gitignore 

Adding to the other answers pointing you to make a .gitignore file...添加到其他答案,指向您制作.gitignore文件...

As a good starting point for any .gitignore file, you can search at https://gitignore.io .作为任何.gitignore文件的良好起点,您可以在https://gitignore.io进行搜索。 Specific settings for 'VisualStudioCode' and various other tools and languages can be entered, and the site will generate a .gitignore file for you.可以输入“VisualStudioCode”和各种其他工具和语言的特定设置,该站点将为您生成一个 .gitignore 文件。

Additional points附加点

  • You can add a .gitignore to any folder to create entries specific to that folder and its nested folders as well.您可以将.gitignore添加到任何文件夹,以创建特定于该文件夹及其嵌套文件夹的条目。

  • By default, Git will not save an empty folder.默认情况下,Git 不会保存空文件夹。 You can put an empty .gitignore file into a folder to force git to save it.你可以将一个空的.gitignore文件放到一个文件夹中,强制 git 保存它。

On your ~/.config/Code/User/settings.json add:在你的~/.config/Code/User/settings.json添加:

"files.exclude": {
        "**/.git": true,
        "**/.svn": true,
        "**/.hg": true,
        "**/CVS": true,
        "**/.DS_Store": true,
        "**/Thumbs.db": true,
        "_ide_helper_models.php": true,
        "_ide_helper*.php": true,
        ".vscode": true,
        ".git/logs": true,
        ".git": true,
        "**/.idea": true,
        ".idea": true,
    },

For those who want to exclude a file vie private ignore using .git/info/exclude .对于那些想要排除文件的人,请使用.git/info/exclude进行私人忽略。 In my case VSCode still kept it as tracked and listed.就我而言,VSCode 仍然保持跟踪和列出。 Even git rm -f --cached <filename> didn't helped at all.甚至git rm -f --cached <filename>根本没有帮助。

What helped instantly was the extension named GIT-EXCLUDE Just had to install it and as I had the file alredy in the exclude file it disappeared right in the moment when the extension started.立即提供帮助的是名为GIT-EXCLUDE的扩展程序,只需安装它,因为我已经在排除文件中拥有该文件,它在扩展程序启动的那一刻就消失了。

It also allows you to use RMB in VSCode file explorer to exclude files conveniently.它还允许您在 VSCode 文件资源管理器中使用 RMB 方便地排除文件。

To exclude a folder from git across all projects on your machine, you can use core.excludesfile configuration Git documentation要在机器上的所有项目中从 git 中排除文件夹,可以使用core.excludesfile配置Git 文档

Create a file called ~/.gitignore_global then add files or folders you would like to exclude like node_modules or editor folders like .vscode创建一个名为~/.gitignore_global的文件,然后添加您想要排除的文件或文件夹,如node_modules或编辑器文件夹,如.vscode

Then run: git config --global core.excludesfile ~/.gitignore_global然后运行: git config --global core.excludesfile ~/.gitignore_global

To ignore a file or directory that is already committed to a Git repository, you can use the following steps:要忽略已经提交到 Git 存储库的文件或目录,您可以使用以下步骤:

  1. Check if the file or directory is tracked by Git: Run the command git ls-files --error-unmatch.vscode .检查文件或目录是否被 Git 跟踪:运行命令 git ls-files --error-unmatch.vscode If the file or directory is tracked, you will see its name printed to the terminal.如果文件或目录被跟踪,您将看到其名称打印到终端。 If it is not tracked, you will see an error message indicating that the file could not be found.如果未被跟踪,您将看到一条错误消息,指出找不到该文件。

  2. Remove the file or directory from the Git repository: If the file or directory is tracked, you can remove it from the Git repository using the command git rm --cached.vscode .从 Git 存储库中删除文件或目录:如果文件或目录被跟踪,您可以使用命令git rm --cached.vscode将其从 Git 存储库中删除。 This will remove the file or directory from Git, but it will not delete it from your local file system.这将从 Git 中删除文件或目录,但不会将其从本地文件系统中删除。

  3. Add the file or directory to the.gitignore file: Open the.gitignore file in your text editor and add the name of the file or directory you want to ignore (eg, .vscode).将文件或目录添加到 .gitignore 文件:在文本编辑器中打开 .gitignore 文件并添加要忽略的文件或目录的名称(例如,.vscode)。 This will tell Git to ignore the file or directory when you commit changes in the future.这将告诉 Git 在您将来提交更改时忽略该文件或目录。

  4. Commit the changes to the.gitignore file: Run the command提交对 .gitignore 文件的更改:运行命令

    git commit -am "Ignore.vscode directory" git commit -am "Ignore.vscode directory"

to commit the changes to the.gitignore file.将更改提交到 .gitignore 文件。 5. Push the changes to the remote repository: If you are using a remote repository, you will need to push the changes to the remote repository using the command git push . 5. 将更改推送到远程存储库:如果您使用的是远程存储库,则需要使用命令git push将更改推送到远程存储库。 This will update the repository on the remote server with the changes you made to the.gitignore file.这将使用您对 .gitignore 文件所做的更改更新远程服务器上的存储库。

Note: If you want to ignore a file or directory that has not yet been committed to the repository, you can simply add it to the.gitignore file and then commit the changes as normal.注意:如果要忽略尚未提交到存储库的文件或目录,只需将其添加到 .gitignore 文件,然后照常提交更改即可。 Git will automatically ignore the file or directory when you commit changes in the future. Git 将在您以后提交更改时自动忽略该文件或目录。

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

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