Git小技 : 過去のコミットをクリアしてGithubに公開する
過去のコミットをクリアしてGithubに公開する
プライベートなリポジトリで開発していたプロジェクトをGithubで公開したい、個人情報やらパスワードなんかが記載されていたファイルだのフォルダだのがあったので履歴をクリアして一からやり直したい。
その度にGithubレポジトリをデリートしたり初期化したりしてきましたが、それなりの手順を見つけたもので。
履歴をクリアしたリポジトリを公開
これで、履歴がクリアされたリポジトリが公開されます。
- // 孤児という意味の--orphanというオプションをつけます
- $ git checkout --orphan production
- $ git add .
- $ git commit -m 'first commit on production'
- // 公開用のリモートリポジトリを追加します
- $ git remote add public https://github.com/[Your Github Account]/production.git
- $ git remote -v
- origin https://github.com/[Your Github Account]/private.git (fetch)
- origin https://github.com/[Your Github Account]/private.git (push)
- public https://github.com/[Your Github Account]/production.git (fetch)
- public https://github.com/[Your Github Account]/production.git (push)
- // productionブランチをpublicにプッシュします
- $ git push public production
プライベートリポジトリで更新した内容で更新
履歴はもちろん一からになりますが、公開用リポジトリが更新されます。
- // masterブランチとproductionブランチの間にtmpブランチを作成
- $ git checkout --orphan tmp
- $ git add .
- $ git commit -m 'first commit on tmp'
- // tmpブランチからproductionブランチに-fオプションをつけてプッシュ
- $ git push -f tmp:production
- // publicに-fオプションをつけてプッシュ
- $ git push -f public production
- // tmpブランチは削除
- $ git branch -d tmp
おわり
気が付けば2年半ぶりの更新でした。今度こそ続きますように…
参照サイト