17
8
お題は不問!Qiita Engineer Festa 2024で記事投稿!
Qiita Engineer Festa20242024年7月17日まで開催中!

シークレット情報をpushして怒られたけど、コミットの消し方がわからない

Last updated at Posted at 2024-06-15

コメント頂き修正しました。ありがとうございます!

初心者過ぎてGCPのクレデンシャル情報をpushしてしまったところ…。

$ git push origin main
Enumerating objects: 33, done.
Counting objects: 100% (33/33), done.
Delta compression using up to 8 threads
Compressing objects: 100% (23/23), done.
Writing objects: 100% (23/23), 7.48 KiB | 7.48 MiB/s, done.
Total 23 (delta 9), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (9/9), completed with 8 local objects.
remote: error: GH013: Repository rule violations found for refs/heads/main.
remote: 
remote: - GITHUB PUSH PROTECTION
remote:   —————————————————————————————————————————
remote:     Resolve the following violations before pushing again
remote: 
remote:     - Push cannot contain secrets
remote: 
remote:     
remote:      (?) Learn how to resolve a blocked push
remote:      https://docs.github.com/code-security/secret-scanning/pushing-a-branch-blocked-by-push-protection
remote:     
remote:      (?) This repository does not have Secret Scanning enabled, but is eligible. Enable Secret Scanning to view and manage detected secrets.
remote:      Visit the repository settings page, https://github.com/sh-go/NenQ/settings/security_analysis
remote:     
remote:     
remote:       —— Google Cloud Service Account Credentials ——————————
remote:        locations:
remote:          - commit: 73111118429ee34e9a081f97ea71f5d81ac276b0
remote:            path: backend/terraform/key.json:1
remote:     
remote:        (?) To push, remove secret from commit(s) or follow this URL to allow the secret.
remote:        https://github.com/sh-go/NenQ/security/secret-scanning/unblock-secret/2hrQpuiDMCyidysiUDLJDZ9SHWe
remote:     
remote: 
remote: 
To https://github.com/sh-go/NenQ.git
 ! [remote rejected] main -> main (push declined due to repository rule violations)
error: failed to push some refs to 'https://github.com/sh-go/NenQ.git'

ちゃんと怒ってくれてpushを止めてくれました。ありがとうGitHub。

書いてある通り、コミットを消すか、シークレット情報をオープンにする許可を出すかのどちらかをしないとpush出来ないそうです。もちろん公開したくはないので、コミットを消してpush出来るようにします。

resetを使おう

resetは文字通りリセットになるので、変更を巻き戻してなかったことにします。

類似したものにrevertがありますが、これは履歴を残しつつ『指定されたコミットを打ち消したよ!』というコミットをします。

なのでコミット履歴が残ってしまい、今回のケースだとpush出来ません。

よって、resetを使いました。先程のエラーメッセージに該当コミットのIDがあるので、それを用いてgit reset 7311111と実行。

よしこれでいける!

あれ?pushがまた通らない?

pushしたところ、また同じエラーが出ました。git log --onelineで確認したところ、

7311111 (HEAD -> main) xxxxxxxxxxxxx
d560811 yyyyyyyyyyyyyy
28fcc81 zzzzzzzzzzzzzz
...

やはりコミットが消えていない…。なぜだ…。

消したいコミットに対して、IDの指定の仕方を間違えてた

数分悩んで調べた結果、gitの公式に説明がありました。

スクリーンショット 2024-06-16 7.50.08.png

文章最後に、

git reset 9e5e6a4を実行するとmasterブランチがコミット9e5e6a4を指すようになります。

とあります。ということは、 指定したID時点を含み、そこまで戻る という動きをするんですね。勉強不足でした…。

ということで、git reset d560811とし、addcommitpushを実行すると無事にpush出来ました!

resetでIDを指定するときは、消したいコミットの一つ前のIDを指定しよう!

revertでIDを指定するときは、消したいコミットのIDになるので注意

17
8
5

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
17
8