LoginSignup
25
13

More than 3 years have passed since last update.

git resetコマンドで任意のpushまで巻き戻す

Last updated at Posted at 2020-08-25

はじめに

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

以上

25
13
0

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
25
13