takafumi blog

日々の勉強メモ

Git入門 その19 / git add -p , git reset -p / 一部だけ変更を反映する & 一部だけ取り消す

環境   git version 1.8.3.1

■ git add -p

変更の一部だけステージングに乗せる。

以下のように-pオプションを追加し、git addする。

$  git add -p hoge.txt
diff --git a/hoge.txt b/hoge.txt
index 67ea492..fa00e52 100644
--- a/hoge.txt
+++ b/hoge.txt
@@ -1,6 +1,6 @@
 0 abcdefghijklmnopqrstuvwxyz
 1 abcdefghijklmnopqrstuvwxyz
-2 abcdefghijklmnopqrstuvwxyz
+change 2 abcdefghijklmnopqrstuvwxyz
 3 abcdefghijklmnopqrstuvwxyz
 4 abcdefghijklmnopqrstuvwxyz
 5 abcdefghijklmnopqrstuvwxyz
Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]?


ハンク毎にステージに乗せるか聞かれるので、対応する文字を打ち込む
※ハンクはファイルの変更部分の単位毎のまとまり。

「?」を打つとヘルプが表示される。

y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk nor any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not stage this hunk nor any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help

英語がパッと読めれば必要ないが、ざっと説明すると

入力 説明
y ハンクをステージに乗せる
n ハンクをステージに乗せない
q コマンドから抜ける。それ以降のハンクは、別ファイルも含め、全てステージに乗せない
a 残りのハンクを全てステージに乗せる
d 残りのハンクを全てステージに乗せない
g ハンクを選択する
/ ハンクに含まれる文字を検索し、そのハンクに移動
j 次の、処理未決定のハンクに移動
J 次のハンクに移動
k 前の、処理未決定のハンクに移動
K 前のハンクに移動
s 現在のハンクをより小さいハンクに分割
e editorを起動して、反映内容を手動で設定する
? ヘルプを表示する

上記は、基本的にはそのファイル内でだけ有効、「q」は除く。
例えば、「/」を選択したとき、検索範囲に含まれるのは、そのファイル内のハンクだけである。

■ git reset -p

変更の一部だけを取り消す。

基本的にadd -pと同じなので、省略する。


takafumi-s.hatenablog.com