gitでファイル変更の一部をコミットする

git

git add -p でファイルの一部をコミットする【Git】

ファイル変更の一部をcommitしたい時のgitコマンド。
ローカル作業用にコードを書き換えている場合などに、余分な変更をStagingにあげなくて済むので便利です。

結論

git add -p で、ファイル変更の一部をStagingにあげる事ができます。

$git add -p

コマンドを実行すると、変更内容が1箇所ずつ表示されていくので
Stagingにあげる場合はy、スキップする場合はnを押して行けばOKです。

diff --git a/db/migrate/20210120142402_sample_migration.rb b/db/migrate/20210120142402_sample_migration.rb
index 81cef41..00a387d 100644
--- a/db/migrate/20210120142402_sample_migration.rb
+++ b/db/migrate/20210120142402_sample_migration.rb
@@ -70,6 +70,18 @@ class SampleMigrations < ActiveRecord::Migration[6.1]
       t.timestamps
     end
 
+    # 投稿カテゴリ
+    create_table :categories do |t|
+      t.string :name, null: false, comment: 'カテゴリ名'
+      t.string :slug, null: false, comment: 'urlスラッグ'
+      t.string :description, null: true, default: '', comment: '説明'
+      t.boolean :is_active, null: false, default: true, comment: '有効フラグ'
+      t.boolean :is_delete, null: false, default: true, comment: '削除フラグ'
+      t.datetime :deleted_at, null: true, comment: '削除日'
+
+      t.timestamps
+    end
+
     # 投稿
     create_table :posts do |t|
       t.references :post_type, null: false
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? 

一部のファイルのみaddしたい場合は、オプションの後にファイルパスを指定することができます。

$ git add -p db/migrate/20210120142402_sample_migration.rb

途中、変更内容のさらに一部をStagingにあげたい場合
eでvimの編集モードに入ることができます。

(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? e
# vimの編集画面

 Manual hunk edit mode -- see bottom for a quick guide.
@@ -70,6 +70,18 @@ class SampleMigrations < ActiveRecord::Migration[6.1]
       t.timestamps
     end

+    # 投稿カテゴリ
+    create_table :categories do |t|
+      t.string :name, null: false, comment: 'カテゴリ名'
+      t.string :slug, null: false, comment: 'urlスラッグ'
+      t.string :description, null: true, default: '', comment: '説明'
+      t.boolean :is_active, null: false, default: true, comment: '有効フラグ'
+      t.boolean :is_delete, null: false, default: true, comment: '削除フラグ'
+      t.datetime :deleted_at, null: true, comment: '削除日'
+
+      t.timestamps
+    end
+
     # 投稿
     create_table :posts do |t|
       t.references :post_type, null: false
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
# marked for staging.
# If it does not apply cleanly, you will be given an opportunity to
# edit again.  If all lines of the hunk are removed, then the edit is
# aborted and the hunk is left unchanged.

英語で以下のような説明が書いてあります。

  • # To remove ‘-‘ lines, make them ‘ ‘ lines (context).
  • # To remove ‘+’ lines, delete them.
  • # Lines starting with # will be removed.
  • -を取り消したい場合 -> ‘-‘を空欄に”
  • +を取り消したい場合 -> その行を消す
  • #と書かれている行は反映されない

試しに、カテゴリ名とURLスラッグ名の行を消してみました
(iでvimのINSERTモードに入れます)

+    # 投稿カテゴリ
+    create_table :categories do |t|
+      t.string :description, null: true, default: '', comment: '説明'
+      t.boolean :is_active, null: false, default: true, comment: '有効フラグ'
+      t.boolean :is_delete, null: false, default: true, comment: '削除フラグ'
+      t.datetime :deleted_at, null: true, comment: '削除日'
+
+      t.timestamps
+    end
+

この状態で、変更をStagingにあげてみます。
(esc -> :wqで、INSERTモードから出て、変更を保存できます)

git diffで、消した行の変更が残っているか確認してみます。

$git diff db/migrate/20210120142402_sample_migration.rb

削除した+行の変更がdiffで表示されていますね。
逆に、Stagingにあげた他の行には+がついていないので、ちゃんと反映されているようです。

diff --git a/db/migrate/20210120142402_sample_migration.rb b/db/migrate/20210120142402_sample_migration.rb
index 2343f43..00a387d 100644
--- a/db/migrate/20210120142402_sample_migration.rb
+++ b/db/migrate/20210120142402_sample_migration.rb
@@ -72,6 +72,8 @@ class SampleMigrations < ActiveRecord::Migration[6.1]
 
     # 投稿カテゴリ
     create_table :categories do |t|
# vimで削除した+の行が、diff(差分)として残っている
+      t.string :name, null: false, comment: 'カテゴリ名'
+      t.string :slug, null: false, comment: 'urlスラッグ'
       t.string :description, null: true, default: '', comment: '説明'
       t.boolean :is_active, null: false, default: true, comment: '有効フラグ'
       t.boolean :is_delete, null: false, default: true, comment: '削除フラグ'

まとめ

ファイル変更の一部をStagingに反映させる、-pオプションを紹介しました。

ファイル単位でのaddコマンドはよく使ってたんですが、
ローカル環境だけの設定とか、テスト用に分岐を追加したりローカル環境のみに残したい変更が溜まっていたのでこのコマンドを知れて作業が楽になりました。

git addした変更を確認したい場合はこちら

変更したファイル名を確認したい場合はこちら

ピックアップ記事

  1. 【Rails】selectメソッドで特定の条件を満たす要素を取得する
  2. git add -p でファイルの一部をコミットする【Git】
  3. 【Blender】起動時に強制終了してしまう問題
  4. 【CSS】おしゃれなラジオボタンを作る
  5. 【Blender】zip版のBlenderをBlender Launcherに移…

関連記事

 
  1. git logで変更したファイル名を確認したい
  2. git

    【git】git checkoutする時に、ベースブランチを指定する

    ワークフローにもよると思うんですが、多人数で開発していてgitを使用す…

  3. git

    git addしたファイルの変更を確認したい【Git】

    git addでStaginにあげたファイルの変更内容を、後から確認し…

  4. git

    【git】remote のURLを後から設定/変更する

    git cloneとか、git remote addなどでpush先の…

カレンダー

2024年4月
1234567
891011121314
15161718192021
22232425262728
2930  

最近の記事

  1. Ruby on Rails

    【Rails】findメソッドで連想配列から指定した値を検索する
  2. 慣れれば3分!Bumpノードを使用した質感表現の方法

    Blender

    【Blender】Bumpを使用した質感表現の方法
  3. Blender

    【Blender】レンダリング結果を新規ウィンドウで開かないようにする
  4. Blender

    【Blender】zip版のBlenderをBlender Launcherに移…
  5. Blenderの複数バージョンを簡単に管理できるBlenderLauncherの使い方

    Blender

    【Blender】複数バージョンを簡単に管理できる、BlenderLaunche…
PAGE TOP