Git:git restoreコマンドでファイルを特定コミットまで戻す方法

スポンサーリンク

git restoreコマンドでファイルを特定コミットまで戻す方法

git restoreコマンドでファイルを特定コミットまで戻すには-sオプションを指定します。

 

オプションの説明

 -s <tree>, --source=<tree>
     Restore the working tree files with the content from the given tree. It is common to specify the source tree by naming a commit, branch or tag associated
     with it.

     If not specified, the contents are restored from HEAD if --staged is given, otherwise from the index.

     As a special case, you may use "A...B" as a shortcut for the merge base of A and B if there is exactly one merge base. You can leave out at most one of A
     and B, in which case it defaults to HEAD.

 

-sオプションを指定した場合

$ cat test.txt
a
b
c
$ git restore -s HEAD^ test.txt  # 1つ前のコミット
$ cat test.txt
a
b
$ git restore -s HEAD~2 test.txt  # 2つ前のコミット
$ cat test.txt
a
$ git restore -s f761ffe test.txt  # 1つ前のコミットのハッシュ値で指定
$ cat test.txt
a
b
$ git restore -s HEAD test.txt  # HEADに戻す
$ cat test.txt
a
b
c

 

-sオプションを指定しない場合(デフォルトはワーキングツリーをHEADに戻す)

$ cat test.txt
a
b
c
$ git restore -s HEAD~2 test.txt # 2つ前のコミット
$ cat test.txt
a
$ git restore test.txt  # オプション指定なしはHEADに戻す
$ cat test.txt
a
b
c