グレインの備忘録

プログラミング関係とかをつらつらと。

Gitで部分コミット・Hunk破棄など

SourceTreeから乗り換えて、生のGitを使っている中でHunkの取り扱いがわからなかったのでメモ。

Hunkのステージ(部分コミット)は git add -p

基本は「-p」オプションを付ければうまくいく。

ステージングの場合においては、

$ git add -p [file]

とすることで、以下のようにHunkごとに取り込むか否かを聞いてくる。

$ git add -p dein_lazy.toml
diff --git a/nvim/dein_lazy.toml b/nvim/dein_lazy.toml
index 848ac2a..56daa89 100644
--- a/nvim/dein_lazy.toml
+++ b/nvim/dein_lazy.toml
@@ -57,4 +57,7 @@ on_i = 1
 [[plugins]]
 #neomake
 repo = 'neomake/neomake'
-
+on_i = 1
+hook_post_source = '''
+   autocmd! BufWritePost * Neomake
+'''
Stage this hunk [y,n,q,a,d,/,e,?]? 

yで取り込み、nで取り込まない、となる。

?を入力すると他のコマンドの意味も出てくる。

y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk or any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not stage this hunk or 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

sでHunkを分割したり、eで編集したりできるようだ。

Hunkのアンステージは git reset -p

当該ファイルにステージ済みのHunkがある場合には、

$ git reset -p [file]

とすることでHunkのアンステージを行える。

操作はgit add -pと同じである。

Hunkの破棄は git checkout -p

Hunkの破棄は

$ git checkout -p [file]

でできる。操作は上2つと同じである。


また、いずれのコマンドにおいても、ファイル名を指定しないと全ファイルを対象にコマンドを実行したのと同じになる。