簡體   English   中英

從 vscode 中的 git 中刪除 node_modules

[英]Remove node_modules from git in vscode

在 git 中刪除node_modules時出錯,vscode 終端

 git rm -r --cached node_modules

錯誤:

致命錯誤: pathspec 'node_modules' 不匹配任何文件

  1. 制作 .gitignore 文件。
  2. 將 node_modules/ 行添加到 gitignore 文件
  3. 運行這個命令git rm -r --cached . git add . git commit -m "remove gitignore files" git push git rm -r --cached . git add . git commit -m "remove gitignore files" git push

我遇到了同樣的問題。 執行以下步驟 -

  • 制作 .gitignore 文件。
  • 在終端中運行以下命令

    git rm -r --cached node_modules

    git commit -am "node_modules be gone!"

    git push origin master

就這樣!

你已准備好出發!

在項目目錄中.gitignore文件。

.gitignore文件看起來像這樣

/Folder_name

在終端或等效命令中鍵入以下命令:

git rm -r --cached Folder_name (remove the directory from git but not delete it from the filesystem/locally)

git commit -m "remove unused directory"

git push origin <your-git-branch> (can be master or develop or others)

創建.gitignore文件並在其中添加node_modules

touch .gitignore && echo "node_modules" >> .gitignore

刪除緩存數據:

git rm -r --cached .

更新你的最后一次提交:

git add .

git commit --amend --no-edit

git push --force-with-lease

該錯誤意味着node_modules目錄不受 git 版本控制。 有2種可能:

  • 它們還沒有添加到Git中。 嘗試運行git status命令,查看它們是否顯示為untracked的文件。

  • 它已被添加到您的gitignore文件中。 要列出所有被忽略的文件,請嘗試以下命令: git ls-files --others -i --exclude-standard

請注意,當前接受的答案不會完全刪除它,它將保留在歷史記錄中。 如果您希望將其從您的歷史記錄中完全刪除,即從該目錄所在的所有過去提交中刪除,您可以使用以下任何一種方式:

git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch node_modules" HEAD

(在此處閱讀更多信息: 7.6 Git 工具 - 重寫歷史

或者,您可以使用名為filter repo的第三方插件——這也是 Git 官方推薦的方法:

git filter-repo --path node_modules --invert-paths

(在此處閱讀有關如何使用的更多信息: 安裝備忘單文檔

然而,在這兩種情況下,請注意您正在重寫歷史,這在團隊中工作時可能是一場噩夢...... 所以要么在你自己的本地分支/環境中使用它,要么確保你知道沒有人有更好的選擇,並讓你所有的伙伴事先知道。

一旦 git 開始跟蹤文件或文件夾,即使您將其添加到 .gitignore 也不會停止

您需要將其從 .git 存儲庫中刪除。 我專門使用本地的,通常位於項目源的根目錄。

截至今天 20200408,似乎沒有鼠標單擊方法可以從 git 中刪除煩人的文件/文件夾。

您將不得不使用終端。

Ctrl-J然后選擇終端選項卡

或從 VSCode 菜單中選擇Terminal / New Terminal

然后發出類似以下命令的內容

git  rm -r --cached  www/index.html

以下排除

 www/ 

已經在我的 .gitignore 文件中了。

在您的情況下,您需要添加“癌症文件夾”:)

node_modules/

PS:簡單的事情變得不必要的困難,我想知道為什么?

下面是我從不使用的無數 Git 命令的垃圾。 享受!

在此處輸入圖像描述

上面的答案很棒。 它們不是你一直記得的東西。 因此,每次需要執行此操作時,您都必須重新在線查看。

這是使用您已經熟悉的 git 命令刪除 node_modules 的快速方法。

  • 首先創建一個.gitignore文件並將/node_modules添加到其中。
  • 然后刪除node_modules文件夾並提交您的更改。

Git 會將其解釋為文件夾刪除,並將從 Github 或您使用的任何源代碼控制中刪除git push上的 node_modules 文件夾。

  • 然后運行npm iyarn install以在您的開發環境中自動從 package.json 重新創建 node_module。

這個解決方案非常適合我在部署 heroku 時遇到的問題,我將 node_modules 推送到 heroku git

  1. 創建文件.gitignore

  2. node_modules/*行添加到gitignore文件

  3. 在您的終端中運行以下命令

    git rm -r --cached node_modules git add. git commit -m "Remove node_modules from git in vscode" git push

您可以從 git 中刪除文件夾(使用命令行或您的 IDE)。
如果要從 git 中刪除node_modules

  1. node_modules重命名為node_modulesx
  2. 犯罪
  3. node_modulesx重命名為node_modules
  4. node_modules添加到 .gitignore (如果需要,創建 .gitignore 文件)
  5. 犯罪

現在您的node_modules文件夾仍然存在,但不會再提交給 git。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM