ファイル変更の一部を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した変更を確認したい場合はこちら
変更したファイル名を確認したい場合はこちら