2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

間違って上げたenvファイルをコミット履歴からも消去する

Posted at

はじめに

メモとして残します。

問題

.envファイルを間違えてHitHubにあげてしまいました。
.envファイルは消したものの、過去のコミット履歴に情報があるのはよくないので、消したいです。

解決方法

基本ドキュメント通りですが、少しはまった箇所がありました。

リポジトリからの機微なデータの削除
https://docs.github.com/ja/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository

git-filter-repoのインストール

xxxyyy@MacBook-Air-2 % brew install git-filter-repo

リポジトリのクローンを作成(最初、元々の作業ディレクトリの中にクローンを作成したが、デスクトップなど、元々のreposiitory_nameの作業ディレクトリとは別のディクトリが良さそう)

xxxyyy@MacBook-Air-2 % git clone --mirror https://github.com/github_name/reposiitory_name.git

クローンしたプログラムのディレクトリに移動

xxxyyy@MacBook-Air-2 % cd reposiitory_name.git

リポジトリの全てのコミット履歴から.envファイルが削除。これにより、過去のコミット履歴に含まれていた.envファイルの情報も完全に削除される。

xxxyyy@MacBook-Air-2 reposiitory_name.git % git filter-repo --path .env --invert-paths

.envファイルが含まれているコミットから、該当ファイルが削除されているか確認

xxxyyy@MacBook-Air-2 reposiitory_name.git % git log --stat

.envファイルが含まれているコミットが存在しないことを確認

xxxyyy@MacBook-Air-2 reposiitory_name.git % git log --all --pretty=format:'%H' --name-only | grep '.env'

.envファイルの履歴を表示。履歴が表示されない場合、.envファイルが削除されている

xxxyyy@MacBook-Air-2 reposiitory_name.git % git log --follow -- .env

forceで上げようとしたところ、エラーになった

xxxyyy@MacBook-Air-2 reposiitory_name.git % git push origin --force --all
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.

remoteとの接続を確認したところ、接続がなかった

xxxyyy@MacBook-Air-2 reposiitory_name.git % git remote -v

remoteを設定

xxxyyy@MacBook-Air-2 reposiitory_name.git % git remote add origin <リモートリポジトリのURL>

forceで上げることができた

xxxyyy@MacBook-Air-2 reposiitory_name.git % git push origin --force --all

おわりに

便利なツールがあってありがたいです。
個人開発だったのでそれほど大変ではなかったですが、複数人で開発していたら大変そうです。
気をつけます。

参考

リポジトリからの機微なデータの削除
https://docs.github.com/ja/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?