0
0

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 3 years have passed since last update.

Git ローカルリポジトリの歴史をリセットする

Posted at

目的

  • 「今コミットした最新コミット取り消したい!!」というときの方法をまとめる

前提情報

  • 筆者が確認した環境を簡単にまとめる

    1. 任意のディレクトリにgit_testディレクトリを作成し、git_testディレクトリに移動して$ git initコマンドを実行した。

    2. 下記コマンドを実行してファーストコミットを実行する。

      $ git commit -m 'first commit' --allow-empty
      
    3. 下記コマンドを実行してgit_testディレクトリ直下にテキストファイルを作成する。

      $ echo 'aaa' > test.txt
      
    4. 下記コマンドを実行して追加したテキストファイルをコミットする。

      $ git add test.txt
      $ git commit -m 'テキストファイルの追加'
      
  • ローカルの最新コミットがコミットメッセージ「テキストファイルの追加」のコミットだとする。

    Git_ローカルリポジトリの歴史をリセットする_md_—_miriwo_qiita.png

  • 最新のコミットをなかったことにしてコミットメッセージ「first commit」のコミットまで状態を戻したい。

方法

  1. ローカルリポジトリ内で下記コマンドを実行してコミットログを出力する。

    $ git log
    
  2. 表示されたログの中でその状態まで戻したいコミットのコミットIDをコピーする。(今回はコミットメッセージが「first commit」のコミットまで戻したいので下記をコピーする。)

    Git_ローカルリポジトリの歴史をリセットする_md_—_miriwo_qiita.png

  3. ローカルリポジトリ内で下記コマンドをして最新のコミットをなかったことにし、IDで指定したコミットまで状態を戻す。

    $ git reset 先程コピーしたコミットID
    
  4. test.txtを登録したコミットがなくなり、text.txtは存在するがgit管理させる前の状態になる。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?