tweeeetyのぶろぐ的めも

アウトプットが少なかったダメな自分をアウトプット<br>\(^o^)/

【git】git pushを取り消す

はじめに

git pushを取り消すメモ。 ほとんど手順メモ程度な感じ+他記事で使うスニペット記事。

とはいえ、数あるgit便利コマンドの中で毎回使うものではないけど
いざって時に役立つ、もしくは、困るのは取り消し系のコマンドですよね。

補足

他の取り消しもぱっと見たい自分用にまとめたので参考までに。

アジェンダ

  1. git resetでmasterへのgit pushを取り消す
  2. git resetでbranchへのgit pushを取り消す
  3. git resetでリモートのみgit pushを取り消す
  4. git revertでgit pushを取り消す

commitの取り消しと操作は似てるのでそちらも参考にしてみてください。
【git】git commitを取り消す

1. git resetでmasterへのgit pushを取り消す

コマンド例

pushしてしまった後がこのログの状態として、
最初のcommitである8189e9c Initial commit戻す例として行います

※ git logを確認
$ git log --oneline
19b4f51 modify index.html
8c76790 add file
8189e9c Initial commit
取り消す

取り消すには、2つ前のcommitを取り消してからpushします。
つまり8189e9c Initial commitだけの状態にする操作。

※ 最初のコミットに戻す(2つぶんコミット取り消し)
$ git reset --hard 8189e9c

$ git log --oneline
8189e9c Initial commit

※ これだけだとエラー
$ git push 
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:tweeeety/some-.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.

※ -fをつけて強制的にpushする
$ git push -f
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:tweeeety/git-some-cancel.git
 + 19b4f51...8189e9c master -> master (forced update)

--hardオプションをつけるとcommitやpushの取り消しだけではなく、
追加したファイルや修正内容も戻ってしまうので、
作業状態を残したいときは--softで行いましょう。

参考 : 【git】git commitを取り消す

2. git resetでbranchへのgit pushを取り消す

コマンド例

pushしてしまった後がこのログの状態として、
最初のcommitである8189e9c Initial commit戻す例として行います

※ ブランチを確認
$ git branch
  master
* some-branch

※ git logを確認
$ git log --oneline
19b4f51 modify index.html
8c76790 add file
8189e9c Initial commit
取り消す

任意のブランチだろうがmasterのときとそれほど変わりはありません。
git push -f origin ブランチ名となるだけですね。

※ 最初のコミットに戻す(2つぶんコミット取り消し)
$ git reset --hard 8189e9c

$ git log --oneline
8189e9c Initial commit

※ -fをつけて強制的にpushする
$ git push -f origin some-branch
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:tweeeety/git-some-cancel.git
 + 19b4f51...8189e9c master -> master (forced update)
github上のCompare $ pull requestボタンも消す

しかし、pushしてしまったcommitは消えますが、
github上のCompare $ pull requestボタンは残ってしまうので消す場合は下記を参考に。
【git】pull requestを出す前 or 出した後の取り消し

3. git resetでリモートのみgit pushを取り消す

1と2ではlocalで取り消し作業を行ってからそれを反映という手順を撮りましたが
localはそのままにリモートのみ取り消すこともできます。

コマンド例

pushしてしまった後がこのログの状態として、
最初のcommitである8189e9c Initial commit戻す例として行います

※ ブランチを確認
$ git branch
  master
* some-branch

※ git logを確認
$ git log --oneline
19b4f51 modify index.html
8c76790 add file
8189e9c Initial commit
取り消す
※ 2つ分のコミットの取り消しをリモートのsome-branchにのみ行う
※ masterのときはgit push -f origin HEAD^^:masterとなります
$ git push -f origin HEAD^^:some-branch

※ リモートのみなのでlocalは作業が残っている
$ git log --oneline
7695581 modify index.html
6e63954 add file
8189e9c Initial commit

4. git revertでgit pushを取り消す

1番目に例としてあげるか迷いましたが、本来はこれで戻すのが一番良いでしょう。
作業履歴という意味では間違って戻すという事も立派な作業のため、
revertすることで打ち消すコミットをしたという履歴を残します。

また、ここでは単純なpushを取り消す例としてmasterで作業してた前提とします。

ブランチ運用でのpush後にpull requestからのmergeを行った場合の取り消しは別途こちらを参考に。
【git】ブランチ運用でpull requestをmerge pullrequestした後に取り消す

コマンド例

間違ってpushしてしまった操作をこんな感じと仮定します。

※ 初期状態
$ ls -l
-rw-r--r--  1 hoge  hoge   18  6  9 21:28 README.md

※ ブランチを確認
$ git branch
* master

※ git logを確認
$ git log --oneline
8189e9c Initial commit

※ ファイルを追加してadd、commit、pushする
$ ls -l 
-rw-r--r--  1 hoge  hoge   18  6  9 21:28 README.md
drwxr-xr-x  3 hoge  hoge  102  6  9 21:19 img
-rw-r--r--  1 hoge  hoge   89  6  9 22:36 index.html

$ git add .
$ git commit -m 'add file'
$ git push 

※ ファイルの中身を修正してadd、commit、push
$ vi index.html
---- vi追記 ----
aiueo←これを1行追加した
---------------
$ git add .
$ git commit -m 'modify index.html'
$ git push 

※ git logを確認
$ git log --online
26f704d modify index.html
8518314 add file
8189e9c Initial commit
取り消す

最後に行ったファイルの修正が間違っていたので戻したいという仮定で取り消します。

※ logを確認
$ git log --oneline
26f704d modify index.html
8518314 add file
8189e9c Initial commit

※ 最後のcommitハッシュに対してrevertする
※ = 最後に行ったcommitを打ち消す逆のcommitをする
$ git revert 26f704d

※ 26f704dで行った修正は取り消されて内容は8518314までの内容に戻っているが
※ 戻したというコミットハッシュとしてc0d8568が作成されている
$ git log --oneline
c0d8568 Revert "modify index.html"
26f704d modify index.html
8518314 add file
8189e9c Initial commit

※ ファイルの中身を見ても追加した`aiueo`は消えている
$ vi index.html

※ revertした(取り消した)コミットをpush
$ git push

まとめ

なんとなーくうまくまとめられなかった感が残りました...

また、実際はブランチ運用でpull requestを出してマージしちゃった後に戻す、
というような操作の方が多そうですがそれはこちらの記事で
【git】ブランチ運用でpull requestをmerge pullrequestした後に取り消す