gitの小ネタ

概要
正直gitのcommitではadd .で全部add!そしてcommit!
という男の全上げ方式を採用していたけど、色々便利なことを知ったのでこれから使っていこう!っていう小ネタをnoteしとこー

勢いよくcommitしたがやり直したい
色々試行錯誤した後、オラッとコミットしたら遊んでたデータとか入っちゃうケースがある。

一つ前のコミットにリセット

git reset HEAD~

慎重にadd

git add -p

各修正ファイルに対して以下の通り判定していくのじゃ!
y: add対象
n: addしない
s: 1ファイル内の変更を分割してadd判定

いざコミット

git commit -m "right commit"


散らかしたブランチのコミットを綺麗にする

試行錯誤してると結構コミットが散らかるので綺麗にしたい
コミットが散らかってる状態

$ git log -4                                                                                                                                  [git-test]
commit fd4864be479e099f402b5bd46d24b99baea25556 (HEAD -> git-test)
Author: Shintaro Uchiyama 
Date:   Sat Nov 24 14:41:34 2018 +0900

   step3

commit eaece8c571d1007bcc13835c3bf44d93e0aa3d24
Author: Shintaro Uchiyama 
Date:   Sat Nov 24 14:20:55 2018 +0900

   step2

commit 788388a8dbf699970363db1d0ca44e42e71bdd23
Author: Shintaro Uchiyama 
Date:   Sat Nov 24 14:19:12 2018 +0900

   sample

commit ba9ee004ec04908c3510257626bb69513f57da04 (origin/master, master)
Author: Shintaro Uchiyama 
Date:   Mon Nov 19 16:33:45 2018 +0900

rebaseで散らかったコミットを治す!

$ git rebase -i ba9ee004ec04908c3510257626bb69513f57da04

1 pick 788388a sample
 2 pick eaece8c step2
 3 pick fd4864b step3
 4
 5 # Rebase ba9ee00..fd4864b onto ba9ee00 (3 commands)
 6 #
 7 # Commands:
 8 # p, pick = use commit
 9 # r, reword = use commit, but edit the commit message
10 # e, edit = use commit, but stop for amending
11 # s, squash = use commit, but meld into previous commit
12 # f, fixup = like "squash", but discard this commit's log message
13 # x, exec = run command (the rest of the line) using shell
14 # d, drop = remove commit
15 #
16 # These lines can be re-ordered; they are executed from top to bottom.
17 #
18 # If you remove a line here THAT COMMIT WILL BE LOST.
19 #
20 # However, if you remove everything, the rebase will be aborted.
21 #
22 # Note that empty commits are commented out


このpickをsに書き換えて保存すると、対象のコミットが1個前のコミットに含まれる!
ここでいうとstep2のpickをsにするとsampleコミットに含められる。
その後でるvimでコメントを書き直すとコミットがきれいになるね!やった!

$ git log -3                                                                                                                                  [git-test]
commit f6cd40ae7a64d1b41e7cf39673931e718d9d85fa (HEAD -> git-test)
Author: Shintaro Uchiyama 
Date:   Sat Nov 24 14:41:34 2018 +0900

   step3

commit eb3c7836914149e065da466236f7c6da579aecfb
Author: Shintaro Uchiyama 
Date:   Sat Nov 24 14:19:12 2018 +0900

   sample

   step2

commit ba9ee004ec04908c3510257626bb69513f57da04 (origin/master, master)
Author: Shintaro Uchiyama 
Date:   Mon Nov 19 16:33:45 2018 +0900

まとめ
stashで退避して動かすみたいなケースが多かったが、これからはしっかりコミットして、reset HEAD~など多用し差分管理を有効に使っていきたい

この記事が気に入ったらサポートをしてみませんか?