kiyasuの日記

ハッピーうれピーよろしく哀愁

gitログにAPI Keyなどprivateであるべき設定ファイルが含まれている場合

git rebase -i HEAD~2

これでHEAD(先頭)から2つ前までのコミットを編集することができます。適当にpickとsquashを入力しましょう。

このような画面が出ます。上が新しく、下が古い。1行目のコミットでAPI Keyを含んでしまっていたので、2行目のコミットでAPI Keyの箇所を削除し、管理対象から外してコミットしてます。この二つを一緒にしてしまえば、API Keyが最初から管理対象から外れているようになります。

squashは前のコミットに押し込むこむことになるので、ここでは1行目にpick, 2行目にsquashとすればOKです。

remoteにも設定を反映させたい

API Keyを持つコミットがすでにremoteにもpushされている場合、場合によっては手遅れな気もしますがこれも同様に削除しましょう。

ただし、rebaseしたローカルのブランチをそのままpushしようとすると、干渉するので

! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/xxxx.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

このように怒られます。

どうすればよいかという

git push -f

もしくは

git push -d origin master

とすればよいようです。どちらにせよ物騒ですが…

λ git push -d origin master
To https://github.com/xxxx.git
! [remote rejected] master (refusing to delete the current branch: refs/heads/master)
error: failed to push some refs to 'https://github.com/xxxxx.git'

と怒られることもあります。masterなのでそれはそうなんですが。

いろいろ原因はあるかもしれませんが、私の場合はremoteブランチでmasterがdefaultブランチになっているからでした。

githubでSettings/Branchesとたどって、デフォルトブランチを変更してから再度push -dするとブランチを削除できました。

そのあと

git push --set-upstream origin master

これでremoteのmasterブランチもrebaseできました。

参考

Gitで一度pushしたブランチでgit commit –amend/git rebase後再度pushする方法 – Kenchant

github - I can't delete a remote master branch on git - Stack Overflow