iT邦幫忙

0

筆記|忘記在專案開頭 git ignore 補救方法

Kim 2023-01-01 14:20:421071 瀏覽
  • 分享至 

  • xImage
  •  

☁️ 背景脈絡

最近正在學習使用 Node.js 搭配 Express 開發網路應用程式,課程有提到不要將 /node_modules 與 .DS_Store (macOS 作業系統創造的隱藏文件) 進行commit。但時隔一段時間再次練習這項專案,我在將專案推送到 github 上才想起來要在一開始 .gitignore ...


✨ 解決方法

忽略資料夾、資料夾中檔案、當前目錄檔案

⚠️ 接下來的情境是「資料夾或檔案已經被追蹤」

  1. 可以使用以下指令來確認有哪些檔案正在被追蹤
$ git ls-tree --name-only --full-tree -r HEAD
  1. 把要忽略的檔案或資料夾寫入 .gitignore 檔案中
/node_modules
/test_folder/tf.html
test.html
  1. -option1 輸入以下指令把根目錄的「所有」檔案、資料夾都移出版本控制(並不會刪除檔案)
$ git rm -r --cached .
  1. -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
  1. 更新 stage (在第二步驟選擇 option1 執行到這邊代表著沒有被加進 .gitignore 的檔案又會被加入追蹤,整體來說它們就像沒有做過更動,再說入 commit 的時候不會看到它們曾經消失在版本控制過)
$ git add .
  1. 更新 commit
$ git commit -m ".gitignore is now working"
  1. 最後推上 github ,遠端資料庫要忽略的檔案就會消失囉!

🙏🏻 參考資料

Apply .gitignore on an existing repository already tracking large number of files → 主要是參考目前讚數 top1 Tohid 和 top4 Eli Nunez 的回答

靠北 .DS_Store 到底是什麼?該不該加入到版本控制內?

[筆記] Git 忽略 .DS_Store 等排除檔案

刪除 .DS_Store 隱藏檔 → 與忽略無關,但有在參考資料看到刪除這個檔案的可能性,所以補在這裡以備不時之需


有任何想法歡迎提出交流:)


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言