はじめに
git resetはチーム開発の場合には無かったことにするため極力使わない方が良いです。
履歴を残しつつ取り消す方法git revertを使えばチームに迷惑にはならないはずです。
使う局面は、自分ひとりで触っている状態などでgit resetする事はあります。
ではgit reset開始。
状態確認
-
pushの一覧を確認します
$ git log --oneline
baa9043 (HEAD -> develop, origin/develop) テキスト変更3
fea4511 テキスト変更2
a40ce6b テキスト変更1
861e1b0 first commit
- 各pushのコミットIDがコミットした内容とともに表示されます。
- 任意のコミットIDの
pushまで巻き戻す- 例えば
first commit時のpushまで巻き戻して、テキスト変更1とテキスト変更2とテキスト変更3を消し去りたい場合
- 例えば
任意のpushまで巻き戻し開始
- 巻き戻したい任意のコミットIDを指定します
- オプションは次項目を参照してください
$ git reset --mixed 861e1b0
Unstaged changes after reset:
M test.txt
- ふたたび
pushの一覧を確認します - 消えました
$ git log --oneline
861e1b0 (HEAD -> develop) first commit
3つのオプション
- ※
git resetには以下の3つのオプションがあります。
$ git reset --soft コミットID 取り消すのはcommit
$ git reset --hard コミットID 取り消すのはcommitとaddとソース
$ git reset --mixed コミットID 取り消すのはcommitとadd
新しくpushする
- では巻き戻されたので新しく
pushいたします
$ git add test.txt
$ git commit -m "テキスト変更4"
$ git push origin develop
! [rejected] develop -> develop (non-fast-forward)
error: failed to push some refs to 'git@github.com:hogehoge/fugafuga.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.
強制的にpushする
- 怒られたので強制的にpushすることになります
-
-fで強制オプション
$ git push -f origin develop
メッセージ省略
To git@github.com:hogehoge/fugafuga.git
+ baa9043...6860cec develop -> develop (forced update)
- 確認します
- 強制
pushができました
$ git log --oneline
6860cec (HEAD -> develop, origin/develop) テキスト変更4
861e1b0 first commit
以上