笔记|忘记在专案开头 git ignore 补救方法

☁️ 背景脉络

最近正在学习使用 Node.js 搭配 Express 开发网路应用程式,课程有提到不要将 /node_modules 与 .DS_Store (macOS 作业系统创造的隐藏文件) 进行commit。但时隔一段时间再次练习这项专案,我在将专案推送到 github 上才想起来要在一开始 .gitignore ...


✨ 解决方法

忽略资料夹、资料夹中档案、当前目录档案

⚠️ 接下来的情境是「资料夹或档案已经被追蹤」

可以使用以下指令来确认有哪些档案正在被追蹤
$ git ls-tree --name-only --full-tree -r HEAD
把要忽略的档案或资料夹写入 .gitignore 档案中
/node_modules/test_folder/tf.htmltest.html
-option1 输入以下指令把根目录的「所有」档案、资料夹都移出版本控制(并不会删除档案)
$ git rm -r --cached .
-option2 根据要忽略的东西输入以下指令:「node_modules 资料夹」
$ git rm -r --cached node_modules/
「test_folder 资料夹的 tf.html 档案」
$ git rm -r --cached test_folder/tf.html
「test.html 档案」
$ git rm -r --cached test.html
更新 stage (在第二步骤选择 option1 执行到这边代表着没有被加进 .gitignore 的档案又会被加入追蹤,整体来说它们就像没有做过更动,再说入 commit 的时候不会看到它们曾经消失在版本控制过)
$ git add .
更新 commit
$ git commit -m ".gitignore is now working"
最后推上 github ,远端资料库要忽略的档案就会消失啰!

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章