コード日進月歩

しんくうの技術的な小話、メモ、つれづれ、など

Gitのcheckoutを使って特定のファイルを前の指定されたコミット状態にもどす

よく忘れるのでメモ

環境

$ git --version
git version 2.21.1 (Apple Git-122.3)

原理

git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] Overwrite the contents of the files that match the pathspec. When the <tree-ish> (most often a commit) is not given, overwrite working tree with the contents in the index. When the <tree-ish> is given, overwrite both the index and the working tree with the contents at the <tree-ish>.

和訳すると

git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] に一致するファイルの内容を上書きします。 <tree-ish>(ほとんどの場合コミット)が指定されていない場合、作業ツリーをインデックスの内容で上書きします。 <tree-ish>を指定したら、インデックスと作業ツリーの両方を<tree-ish>の内容で上書きします。

やり方

例えば test.rbmaster ブランチの状態に戻したい場合は

$ git checkout master test.rb

とやればいい

参考リンク