[GIT] 特定のファイルの変更履歴や過去のデータ内容を確認する方法

プログラムのコーディングをしていると、以前書いたスニペットを、後で使えるかもしれないと思ってコメントアウトして残しておく人は、僕です。
これ、他のプログラマーの人に見られると、なんだか恥ずかしくないですか?
僕も人のこういうのを見た時に、「邪魔だな〜」って感じます。
が、自分もやります。
それは、消すのが恐いからなんですね。
消した後で戻す時に、コメントアウトしてあるだけだと便利なんですね〜。
でもそんなタイミングってほとんどありませんよ。
そういう事良くわかってるんですが、消すのが恐いんですよ。
そうだ!GITを使おう!!
今や開発プログラムでは当たり前になったGITを使えば、消すのが怖く無くなるのではないか・・・と思ったんですが、
いざ、特定のファイルの以前のバージョンを確認するのって、逆に面倒くさくないのか?
という疑問もあり、実際のコマンドも調べておかないと本番で使えないし、消す勇気が持てないので、調べておきました。
ちなみに、GITは何かっていう説明は今回はしません。
以前の記事を参考にしてください。
Git操作まとめ #1「初期設定」
GITコマンドで特定ファイルの履歴を調べる
通常の履歴を調べるのは「log」だけでいいんですが、特定ファイルの場合は、「-p」オプションをつけて、ファイルのpathを入れるだけです。
通常の履歴「git log」
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
$ git log -- commit 2d51176856f6d479f2de046a0af2660ff3f4dd4c Author: yugeta.koji <geta1972 @gmail.com> Date: Sun Jul 23 09:26:15 2017 +0900 003 commit 401ccdd47da9c570dc605c6bb4932c7510f6cda9 Author: yugeta.koji </geta1972><geta1972 @gmail.com> Date: Sun Jul 23 09:25:54 2017 +0900 002 commit 652112687910e389140a849d626cf18f929db72a Author: yugeta.koji </geta1972><geta1972 @gmail.com> Date: Sun Jul 23 09:25:35 2017 +0900 001 </geta1972> |
特定ファイルの履歴「git log -p %path%」
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
$ git log -p test.dat commit 2d51176856f6d479f2de046a0af2660ff3f4dd4c Author: yugeta.koji <geta1972@gmail.com> Date: Sun Jul 23 09:26:15 2017 +0900 003 diff --git a/test.dat b/test.dat index 7c5b7c7..1752366 100644 --- a/test.dat +++ b/test.dat @@ -1,2 +1,3 @@ test-001 -test-002 +remove-002 +add-003 commit 401ccdd47da9c570dc605c6bb4932c7510f6cda9 Author: yugeta.koji <geta1972@gmail.com> Date: Sun Jul 23 09:25:54 2017 +0900 002 diff --git a/test.dat b/test.dat index 39a9842..7c5b7c7 100644 --- a/test.dat +++ b/test.dat @@ -1 +1,2 @@ -test-001 \ No newline at end of file +test-001 +test-002 commit 652112687910e389140a849d626cf18f929db72a Author: yugeta.koji <geta1972@gmail.com> Date: Sun Jul 23 09:25:35 2017 +0900 001 diff --git a/test.dat b/test.dat new file mode 100644 index 0000000..39a9842 --- /dev/null +++ b/test.dat @@ -0,0 +1 @@ +test-001 \ No newline at end of file |
フォルダにも使える「git log -p %dir%」
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
git log -p ./ commit 2d51176856f6d479f2de046a0af2660ff3f4dd4c Author: yugeta.koji <geta1972@gmail.com> Date: Sun Jul 23 09:26:15 2017 +0900 003 diff --git a/test.dat b/test.dat index 7c5b7c7..1752366 100644 --- a/test.dat +++ b/test.dat @@ -1,2 +1,3 @@ test-001 -test-002 +remove-002 +add-003 commit 401ccdd47da9c570dc605c6bb4932c7510f6cda9 Author: yugeta.koji <geta1972@gmail.com> Date: Sun Jul 23 09:25:54 2017 +0900 002 diff --git a/test.dat b/test.dat index 39a9842..7c5b7c7 100644 --- a/test.dat +++ b/test.dat @@ -1 +1,2 @@ -test-001 \ No newline at end of file +test-001 +test-002 commit 652112687910e389140a849d626cf18f929db72a Author: yugeta.koji <geta1972@gmail.com> Date: Sun Jul 23 09:25:35 2017 +0900 001 diff --git a/test.dat b/test.dat new file mode 100644 index 0000000..39a9842 --- /dev/null +++ b/test.dat @@ -0,0 +1 @@ +test-001 \ No newline at end of file |
任意の時点でのファイルの中身を表示する「git cat-file -p %key%:%file%」
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# log-1 $ git cat-file -p 65211268791:test.dat test-001 # log-2 $ git cat-file -p 401ccdd47da9c57:test.dat test-001 test-002 # log-3 $ git cat-file -p 2d51176856:test.dat test-001 remove-002 add-003 |
GIT管理は開発プロセスの乱雑さも見えてしまう
GITを使って開発を進めていくと、ちゃんと管理できていると、元の記述にもどしたり、以前書いたコードを参照するのが非常にスムーズに行なえます。
だけど、ファイル名やフォルダ名などを、その時の気分でコロコロ変えたりしていると、今回のような履歴の確認などがかなり煩雑になります。
そして、今回はコメントアウトして古いソースコードを残しておくケースをあげましたが、bakファイルを作っておくというケースも少なくないようです。
フォルダの中に、沢山のバックアップファイル(拡張子が.bakや.orgなど)で溢れかえっている人いませんか?
もちろん、僕ですwww
こういう状態は言語道断ですね。せっかくGITを使っているんですから、スマートに行きましょう。
そして、ちゃんと設計してファイル名、フォルダ名などの変更も少なく出来うようになれば、より効率的な開発ができるのではないでしょうか?
コメントアウトが癖になっている人、自分のタスクも後回しにしている人が多いのではないですか?