3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

gitでちょっと前のコミットに戻りたい時

Last updated at Posted at 2018-07-10

はじめに

開発途中で、「ん?デグってるっぽいな」と感じた時、
自分のせいじゃないと確信するために、過去のコミットに戻って、ビルドして動きを確かめる時の流れをメモとして残しておきます。

実行するgitコマンド

1.stashの作成

$ git stash       # 現在編集中でコミットしていないファイルをtemp領域に移動させる
$ git stash list  # あるか確認

2.過去に移動

$ git log --oneline      # どのコミットに戻るか吟味 自分のコミット毎に試してます。
$ git checkout 25426c3c8 # git logで選んだコミットに戻る
$ git show               # 戻ったよね確認

3.ビルドして、動きを確かめたり何やら

ああ、やっぱり自分が産んだデグレか

4.現在に戻る

$ git checkout -f develop # ブランチ名は環境によりますが。
$ git show                # 戻ったよね確認

5.stashの復元

$ git stash pop  # これでコミット前のファイルも戻ってきます。

終わりに

最初はgit stashの存在を知らなくて、かつ「checkout -f develop」で編集中のファイルを失うことを知らず
、、、かなり痛い目を見ました。

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?