Node.js 个模块推送到 GitHub

Node.js modules pushed to GitHub

我正在尝试将我的项目推送到 GitHub 和 Heroku。因为我在我的应用程序中使用 Node.js,所以我有文件夹 node_modules。在提交之前,我创建了一个名为 .gitignore 的文件来忽略 Node.js 模块。

我在文件中添加了文件夹 node_modules 并尝试从 comandline/Terminal 推送到 GitHub 存储库。我没想到 Node.js 模块会被推送,但它们最终出现在存储库中。有没有我必须遵循的方法?

这是我解决问题的方法

 - $ git rm -r --cached node_modules

将文件夹 node_modules/ 添加到您的 .gitignore 文件:

 - $ git commit -m "remove the ignored directory node_modules"

然后

 - $ git push origin master

之后,如果您刷新 GitHub 页面,您将看到 node_modules 文件夹消失了。

为了以防万一,.gitignore 文件是在 node_modules 中还是在项目的根目录中?

如果是这样,我建议将它(至少)放在父文件夹中,或者最好放在项目的根目录中,并在其中调用 node_modules 作为文件位置的相对路径.即:some-path/node_modules.

此外,一旦文件夹被提交和推送,你应该忽略 .gitignore 文件(通过暂时删除它或删除 node_modules 行),所以Git 可以跟踪文件、删除文件夹、提交、推送和恢复 .gitignore 文件。

您必须告诉 Git 取消跟踪已添加到 Git 的文件(在您忽略它们之前):

git rm --cached FILE

FILE 不会从您的文件系统中删除。 Git 将取消跟踪此 fille,由于它被忽略,因此不会再被跟踪。

然后,您当然必须提交并推送此更改。

首先,您需要将 node_modules 添加到您的 .gitignore 文件。

然后

$ git rm -r -chached node_modules
$ git commit -m "remove node_modules"

然后:

$ git push origin master

应将 node_modules 文件夹添加到 .gitignore 文件。

首先,摆脱你的 node_modules:

$ git rm -r --cached node_modules  
$ git commit -m "remove node_modules"

然后,推送您的更改

$ git push origin master

刷新页面查看预期效果。

在您的目录中创建一个 .gitignore 文件并插入:

.gitignore
node_modules

它非常适合我。对于新手编码人员来说,这也是摆脱 Node.js 模块的一种更简单的方法。