LoginSignup
1

More than 1 year has passed since last update.

commitしたファイルや変更箇所は"git show"で確認できる

Posted at

はじめに

変更を加えるたびにcommitするように意識をしているのですが、commitしたファイル内の変更箇所を確認したくなる時がありました。
そういえば確認の仕方を知らないなと思ったので、調べたことを備忘録として残そうと思います。

git showで確認できる

通常のgit logを叩いても、commitメッセージは表示されますが、commit内のファイルや変更箇所は表示されません。
どのファイルがcommitに含まれているか、ファイル内の変更箇所は何かを確認する際には"git show"コマンドを使用します。

そのままターミナルで"git show"コマンドを叩けば、下記のように最新のcommit内のファイル/変更箇所を表示してくれます。

~ % git show
commit a16cbf9dd299d41dfff4cab3ab9452550b091df6
Author: ~
Date:   Thu Sep 8 23:06:14 2022 +0900

    [update]jsonの記述を修正 #7
    
    updateが失敗した時のjsonデータの記述が間違っていたため修正。

diff --git a/backend/app/controllers/api/v1/quizzes_controller.rb b/backend/app/controllers/api/v1/quizzes_controller.rb
index 4c8ca22..e6584a9 100644
--- a/backend/app/controllers/api/v1/quizzes_controller.rb
+++ b/backend/app/controllers/api/v1/quizzes_controller.rb
@@ -1,6 +1,6 @@
 class Api::V1::QuizzesController < ApplicationController
   before_action :authenticate_api_v1_user!, only: [:create, :update, :destroy]
-  before_action :set_quiz, only: [:update, :destroy]
+  before_action :set_quiz, only: [:show, :update, :destroy]
 
   def index
     quizzes = Quiz.all
@@ -20,7 +20,7 @@ class Api::V1::QuizzesController < ApplicationController
     if @quiz.update(quiz_params)
       render json: { status: 'SUCCESS', data: @quiz }
     else
-      render json: { status: 'SUCCESS', message: 'Not updated', data: quiz.errors }
+      render json: { status: 'SUCCESS', message: 'Not updated', data: @quiz.errors }
     end
   end
 

HEADでcommitを指定

下記のようにHEADを使用することでcommitを指定することもできます。

#最新のコミットを表示(git showと変わりません)
git show HEAD

#一つ前のcommitを表示
git show HEAD~1

#n個前のcommitを指定
git show HEAD~n

commit idでcommitを指定

最新から数えるのが大変な場合は、"commit id"で指定することも可能です。

git show <commit id>

#例
git show a16cbf9dd299d41dfff4cab3ab9452550b091df6

ちなみにcommit idは、git logなどで確認できます。

~ % git log
commit 16299cf986cfa40cf475ec5dc1d1d0c52a73a110 (HEAD -> ブランチ名)
#commit 以降がcommit idです!

内容が気になった時にサクッと確認できますね。
gitコマンドは使って覚えるのが一番早いと思うので、どんどん手を動かして覚えていきたいです!

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