LoginSignup
1
2

編集の差分を扱う git diff, git status, git checkout

Last updated at Posted at 2024-03-19

概要

本番環境で編集した時に差分を見ることができる git コマンドを紹介します。
ファイル内を編集することなく、差分を解消して元の状態に戻してくれるgitも紹介します。

対象者

  • 差分をみたい方
  • 差分を解消したい方

紹介するgitコマンド

ファイルの編集結果をすべて見たい場合は
git status

1つのファイルの差分を見たい場合は
git diff
git diff -- **ファイル名**

1つのファイルの差分を解消して、編集を無かったことにしたい場合は
git checkout
git checkout -- **ファイル名**

注意点
実行する際は、ディレクトリを該当リポジトリに移動してから行いましょう。

git status

  • すべての差分を modified: ****.rb 形式で表示してくれます
  • config/application.rb のようにディレクトリも一緒に表示してくれます
ディレクトリ移動
ubuntu@ip-111-11-11-111:..$ cd /project/aaa
本番環境:実行
ubuntu@ip-111-11-11-111:/project/aaa$ git status
本番環境:結果
ubuntu@ip-111-11-11-111:/project/aaa$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   Gemfile.lock
	modified:   config/application.rb
    modified:   app/services/get_amazon_info_service.rb

git diff

ディレクトリ移動
ubuntu@ip-111-11-11-111:..$ cd /project/aaa

すべての差分表示

  • すべての差分の具体的な内容を表示してくれます
  • a b のパターンに別けて差分を表示してくれます
実行
ubuntu@ip-111-11-11-111:/project/aaa$ git diff
結果
ubuntu@ip-111-11-11-111:/project/aaa$ git diff

diff --git a/Gemfile.lock b/Gemfile.lock
index 609f***..******d 100***
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -186,6 +186,7 @@ GEM
       actionmailer (>= *.*)
       letter_opener (~> *.*)
       railties (>= *.*)
+    libv8-node (**.**.*.*)
     libv8-node (**.**.0.0-x86_64-darwin-18)

 
diff --git a/app/services/get_amazon_info_service.rb 
b/app/services/get_amazon_info_service.rb
index aaf0***..e47**** 100***
--- a/app/services/get_amazon_info_service.rb
+++ b/app/services/get_amazon_info_service.rb
@@ -5,7 +5,7 @@ module AmazonInfo
   class GetAmazonInfoService < ::BaseService 
-    LIMIT = 40
+    LIMIT = 50

ファイル指定して差分表示

  • -- app/services/get_amazon_info_service.rb 形式でファイルを指定できます
実行
ubuntu@ip-111-11-11-111:/project/aaa$ git diff -- app/services/get_amazon_info_service.rb
結果
ubuntu@ip-111-11-11-111:/project/aaa$ git diff -- app/services/get_amazon_info_service.rb
 
diff --git a/app/services/get_amazon_info_service.rb 
b/app/services/get_amazon_info_service.rb
index aaf0***..e47**** 100***
--- a/app/services/get_amazon_info_service.rb
+++ b/app/services/get_amazon_info_service.rb
@@ -5,7 +5,7 @@ module AmazonInfo
   class GetAmazonInfoService < ::BaseService 
-    LIMIT = 40
+    LIMIT = 50

git checkout

ディレクトリ移動
ubuntu@ip-111-11-11-111:..$ cd /project/aaa
実行
ubuntu@ip-111-11-11-111:/project/aaa$ git checkout -- app/services/get_amazon_info_service.rb
結果(画面上は何も起きません)
ubuntu@ip-111-11-11-111:/project/aaa$ git checkout -- app/services/get_amazon_info_service.rb
ubuntu@ip-111-11-11-111:/project/aaa$ 
  • git diff をしても差分が表示されなくなります
検証(差分がないため表示されない)
ubuntu@ip-111-11-11-111:/project/aaa$ git diff -- app/services/get_amazon_info_service.rb

一言

  • git checkoutはブランチ移動にも使います。なぜか2つの役割を持っていたので、2019年8月にリリースされた Git 2.23 からは git switchでのブランチ間移動が推奨されています。
1
2
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
1
2