Github - 如何刪除GitHub上所有的 commit(一)

How to remove all history commit and push again?

大家應該都有一個經驗,在程式或是網頁的檔案都準備就緒,repository 也創好之後,快樂的下了 git push 指令上去 Github,按下 enter 的瞬間就會發現好像忘記加 .gitignore 了,所有的資料庫帳號密碼私密的 config 設定都上去了…。本文就來介紹如何快速的刪除一開始的 commit 與重新上傳的方法,當什麼事都沒發生過!

在本範例你會學到:

  • 如何快速修正上傳程式時不小心帶入機密資料的問題,如:
    • database 帳號密碼
    • 相關服務的 token (slack, sentry)
    • 網頁 ip 位置
    • 通訊阜 port

在本範例你需要先準備好:

  • 有基礎的使用 Git 的能力

1.刪除 .git

提醒執行此步驟時請確定有對資料進行備份

  • 此步驟會刪除所有的紀錄,請斟酌使用,並不適合針對多個 commit 的某個 commit 刪除,只適合一開始創立的時候。
cat .git/config
rm -rf .git

2.重新 commit 正確的檔案

  • 先確認機密資料的 config 檔已經加到了 .gitignore 中(ex: inienv檔)
  • 重新上傳檔案
git init
git add .
git commit -m "Initial commit"

3.重新 push 檔案

  • 重新 push
  • 查看頁面是否有將機密資訊刪除了
git remote add origin <github-uri>
git push -u --force origin master

到這邊就大功告成,之後有空會再分享,已經存在一段時間的專案,真的有需要刪除的 commit 該如何進行,我們下次見!

參考資料

本篇參考除了最佳答案以外,也有另外幾個分數也很高的解法,各位也可以嘗試看看。

https://stackoverflow.com/questions/9683279/make-the-current-commit-the-only-initial-commit-in-a-git-repository

若有任何問題與指教歡迎與我聯繫,若覺得我的內容不錯麻煩幫我隨便點個廣告,謝謝。


 上一篇
Github - 如何使用rebase移除GitHub上最新或特定的 commit(二) Github - 如何使用rebase移除GitHub上最新或特定的 commit(二)
How to remove the last commit and froce push again?在前一篇 Github - 如何移除GitHub上所有的 commit(一) 的系列文中介紹到了如何完整個將整個 repository 的
2020-03-22
下一篇 
Flask 系列 - Flask error handling 教學(二) Flask 系列 - Flask error handling 教學(二)
Example of error handling in flask. (Part II).在本範例你會學到: 簡易的 Flask API 架設 (上篇) 使用 Flask lib 的 abort 控制錯誤訊息 (下篇) 使用 trac
2020-02-19
  目錄