helen's blog

ずっとおもしろいことしてたいな。

リモートに追跡してほしくないファイルをpushしちゃったとき

  • push済み
  • しかも2個前のコミットだからresetはしたくない
  • revertして恥晒ししたくない

という依頼を受けたので

自分で試してみた

# その人の作業ブランチをチェックアウト
$ git checkout feature/branch-a

# そこからブランチ派生
$ git checkout -b branch-helen
Switched to a new branch branch-helen

# 修正したいコミットをpickからeditに
$ git rebase -i HEAD~3
Stopped at needed_fix_commit
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run

$ git rebase --continue

# 確認
$ git status
rebase in progress; onto hogege
You are currently editing a commit while rebasing branch 'branch-helen' on 'hogege'.
(use "git commit --amend" to amend the current commit)
(use "git rebase --continue" once you are satisfied with your changes)
nothing to commit, working directory clean

# rebase中になっている
# "git commit --amend"で修正して"git rebase --continue" で確定される

# pushしたくなかったファイルを元に戻す

$ git add 
$ git commit -m "これで消えたらいいな" # アッ

$ git branch
* (no branch, rebasing branch-helen) # アッ
branch-a
branch-helen
master

$ git rebase --continue # なぜここまで気づかなかったのか自分
Successfully rebased and updated refs/heads/branch-helen

$ git push origin
Counting objects: 166, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (99/99), done.
Writing objects: 100% (166/166), 31.14 KiB | 0 bytes/s, done.
Total 166 (delta 90), reused 94 (delta 25)

アレッ
コミットが割り込んだぞ....
amendしてないし別ユーザーだからうまく行かないのかもしれない...

ご本人に修正していただきました

突然送りつけたコマンドたち

# リモートへのpush取り消し
# ローカルには残る
$ git push -f origin HEAD~~:hoge_branch
 
# コミットの修正
$ git rebase -i HEAD~~
 
# 編集
 
$ git commit --amend
$ git rebase --continue
 
# 再度push
$ git push origin

すると...
コミットの編集に成功しました!

ちなみにコミット時刻は変更なしでコミットIDは変わっていました(rebaseのせいっぽい)

$ git push -f origin HEAD~~:hoge_branch

リモートのHEADを指定の位置に戻せる
リモートのみなのでローカルには残る
そしてコミットを改竄してpushするという荒技でした