[Git] git add, commit, push の操作をそれぞれの段階で取り消す方法
以下をそれぞれの段階で cancel, rollback したい。
[markdown]
* git add
* git commit
* git push
## add を取り消す
`git checkout`
add したファイルをステージから削除する。
コミット前。
“`prettyprinted
% git checkout ファイル名
“`
> * [.gitignore に書き忘れたファイルをステージ(git add)してしまったので、取り消したい | deadwood](https://www.d-wood.com/blog/2013/09/11_4598.html)
> * [ファイルを特定のコミット状態まで戻す | deadwood](https://www.d-wood.com/blog/2013/08/28_4518.html)
こちらの方が直感的か。
“`prettyprinted
% git reset HEAD — ファイル・ディレクトリ名
“`
## commit を取り消す
`git reset`
指定したコミットまで戻る。
オプション `–hard` でファイルの状態も戻る。
“`prettyprinted
% git reset –hard 6cc6cbe939b62d878816ef917aabfe39f77b0bd0
“`
### commit を修正する場合
取消して、修正したい。
`git commit –amend`
コメントを間違えたり、ファイルを add し忘れた場合、これを書き換える。
“`prettyprinted
% git commit –amend -m ‘fixed issue #1, AppleScript に全ての処理をまとめ、空白 のパスを cd できるよう修正した。’
“`
> * [Gitの忘れがちだけど絶対に使うであろうコマンド達 – Git可愛いよGit](http://tech.basicinc.jp/Git/2013/06/15/git_command/)
> * [git commitをやり直しする&取り消しする(「get commit –amend」と「git reset」) – hogehoge foobar Blog Style5](http://d.hatena.ne.jp/mrgoofy33/20100910/1284069468)
### commit の取消を間違えた場合
`git reflog` で確認。
“`prettyprinted
% git reflog
“`
間違えた操作を `git reset` で戻す。
“`prettyprinted
$git reset –hard HEAD@{1}
“`
> * [git rebase を undo したい | deadwood](https://www.d-wood.com/blog/2013/09/05_4571.html)
### git revert
このあたりの使い分けがまだ分かっていない。
> * [2. revert【チュートリアル3 コミットを書き換えよう!】 | サルでもわかるGit入門 〜バージョン管理を使いこなそう〜 | どこでもプロジェクト管理バックログ](http://www.backlog.jp/git-guide/stepup/stepup7_2.html)
> * [git revert で複数コミットを打ち消す – miauの避難所](http://d.hatena.ne.jp/miau/20100709/1278699637)
## push を取り消す
`git push -f`
上記の手順でローカル master を修正したが、リモート origin に push されているので、これも取り消す。
“`prettyprinted
% git push -f origin master
“`
force push する。
> * [間違えてcommitしたファイルをリポジトリから削除する | deadwood](https://www.d-wood.com/blog/2013/09/11_4600.html)
### リモートの push のみを取り消す
ローカル上のコミットを残して、github の push を取り消したいときは、このようにするそう。
“`prettyprinted
% git push -f origin HEAD^:master
“`
> * [githubにgit pushした変更の取り消し – Hack Your Design!](http://blog.toshimaru.net/git-pushgithub/)
> * [gitで間違ってcommit or pushしたのを取り消す – Qiita](http://qiita.com/cutmail/items/3d786c0e77dfb5cd1e3c)
### リモートの push とローカルの変更を取り消す
が、自分の環境では動かなかったので、ローカルの変更は消えてしまいますが下記で対応。
“`prettyprinted
% git rebase -i HEAD~2
% git push origin +master
“`
## 実例
状況。
“`prettyprinted
% git log
commit 9e06cb940e61e8c35f762457d77909b1d1008589
Author: DriftwoodJP <****@gmail.com>
Date: Sun Feb 9 17:52:36 2014 +0900
Revert “AppleScript に全ての処理をまとめ、空白のパスを cd できるよう修正した
This reverts commit 6cc6cbe939b62d878816ef917aabfe39f77b0bd0.
commit 6cc6cbe939b62d878816ef917aabfe39f77b0bd0
Author: DriftwoodJP <****@gmail.com>
Date: Sun Feb 9 17:35:58 2014 +0900
AppleScript に全ての処理をまとめ、空白のパスを cd できるよう修正した。
commit 0983977d31af10a184a32e82e3b98eb4382e91e3
Author: DriftwoodJP <****@gmail.com>
Date: Fri Dec 27 17:33:43 2013 +0900
added OpenInTerminal
commit cac399da0e1a9141b307c7eae43c60cfff0f4598
“`
1つめのコミットを取り消して、2つめのコミットのコメントを変更する。
“`prettyprinted
% git reset –hard 6cc6cbe939b62d878816ef917aabfe39f77b0bd0
HEAD is now at 6cc6cbe AppleScript に全ての処理をまとめ、空白のパスを cd できるよう修正した。
% git commit –amend -m ‘fixed issue #1, AppleScript に全ての処理をまとめ、空白 のパスを cd できるよう修正した。’
[master 66413c8] fixed issue #1, AppleScript に全ての処理をまとめ、空白のパスを cd できるよう修正した。
3 files changed, 128 insertions(+), 511 deletions(-)
rewrite OpenInTerminal.app/Contents/document.wflow (66%)
create mode 100644 “OpenInTerminal.app/Icon\r”
“`
github へ force push する。
“`prettyprinted
% git push -f origin master
Counting objects: 15, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (9/9), 1.29 KiB | 0 bytes/s, done.
Total 9 (delta 4), reused 0 (delta 0)
To git@github.com:DriftwoodJP/automator-workflows.git
+ 9e06cb9…66413c8 master -> master (forced update)
“`
“`prettyprinted
% git log
commit 66413c817c3ee696138bf801835784250ae882c1
Author: DriftwoodJP <****@gmail.com>
Date: Sun Feb 9 17:35:58 2014 +0900
fixed issue #1, AppleScript に全ての処理をまとめ、空白のパスを cd できるよう
commit 0983977d31af10a184a32e82e3b98eb4382e91e3
“`
## 補遺
> * [Git初心者が絶対に覚えておくべきコマンド – idesaku blog](http://d.hatena.ne.jp/idesaku/20091106/1257507849)
[/markdown]