Git小技 : 過去のコミットをクリアしてGithubに公開する

過去のコミットをクリアしてGithubに公開する

プライベートなリポジトリで開発していたプロジェクトをGithubで公開したい、個人情報やらパスワードなんかが記載されていたファイルだのフォルダだのがあったので履歴をクリアして一からやり直したい。

その度にGithubレポジトリをデリートしたり初期化したりしてきましたが、それなりの手順を見つけたもので。

 

履歴をクリアしたリポジトリを公開

これで、履歴がクリアされたリポジトリが公開されます。

  1.  
  2. // 孤児という意味の--orphanというオプションをつけます
  3. $ git checkout --orphan production
  4. $ git add .
  5. $ git commit -m 'first commit on production'
  6.  
  7. // 公開用のリモートリポジトリを追加します
  8. $ git remote add public https://github.com/[Your Github Account]/production.git
  9. $ git remote -v
  10. origin https://github.com/[Your Github Account]/private.git (fetch)
  11. origin https://github.com/[Your Github Account]/private.git (push)
  12. public https://github.com/[Your Github Account]/production.git (fetch)
  13. public https://github.com/[Your Github Account]/production.git (push)
  14.  
  15. // productionブランチをpublicにプッシュします
  16. $ git push public production
  17.  

 

プライベートリポジトリで更新した内容で更新

履歴はもちろん一からになりますが、公開用リポジトリが更新されます。

  1.  
  2. // masterブランチとproductionブランチの間にtmpブランチを作成
  3. $ git checkout --orphan tmp
  4. $ git add .
  5. $ git commit -m 'first commit on tmp'
  6.  
  7. // tmpブランチからproductionブランチに-fオプションをつけてプッシュ
  8. $ git push -f tmp:production
  9.  
  10. // publicに-fオプションをつけてプッシュ
  11. $ git push -f public production
  12.  
  13. // tmpブランチは削除
  14. $ git branch -d tmp
  15.  

 

おわり

気が付けば2年半ぶりの更新でした。今度こそ続きますように…

 

参照サイト