LoginSignup
1
0

More than 1 year has passed since last update.

【git reflog】gitでやらかしてもgitなら大丈夫

Posted at

やらかし内容

  • git rebase --hard で必要な変更も消してしまった。

    • Source Treeだと現在のブランチをこのコミットまでリセット(ブランチ名)をこのコミットまで戻すをHardで実行
  • 必要なブランチを削除してしまった

  • など。。。

git reflogとは?

git reflogとは、Gitの操作履歴を見ることができるコマンド

$ git reflog
c2344c123 (HEAD -> develop, origin/develop, origin/HEAD) HEAD@{0}: pull origin develop: Fast-forward
b452355c0 HEAD@{1}: checkout: moving from master to develop
d52edfc46 HEAD@{2}: checkout: moving from develop to master
b153446c9 HEAD@{3}: reset: moving to b953044c833ad515f06be9ba9c32111a353b65f3

上記のログだと↓のようにGit操作が行われている。

  • b953044c833ad515f06be9ba9c32111a353b65f3のコミットにリセット
  • developブランチからmasterブランチにチェックアウト
  • masterブランチからdevelopブランチにチェックアウト
  • developブランチでpull

やらかした操作をリセットする

先ほどのgit reflogで操作履歴を確認後、git reset --hardを使いやらかした操作の一つ前に戻ることでやらかしの前に状態を戻すことができる。

git reset --hard HEAD@{1}

まとめ

  • 追加したファイルをプッシュする前にgit resetで消してしまった
  • プッシュ前にブランチを削除してしまった

など、Gitでやらかしてしまった場合でも、Gitなら大体復元することができます。
困った時はgit reflogを使ってみてください。

1
0
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
1
0