Table of Contents
Commit Guideline
- Commit Messageは自分を含むチームメンバーが一目で, そのcommitがどのような変更を加えたものなのか理解できるものであるべき
ルールが複雑だと運用が辛いので, 一旦シンプルに以下のルールで運用しています:
- 作成されたcommitがどのような分野の作業なのか判別するためPrefixを付与する
- Prefixが2つ以上になる場合は, ENH/DEPR:のようにスラッシュを加える
- commit内容の一文サマリをメッセージに加える
- Issue番号が加えられるなら加える
- git commit -mで良い
▶ Prefix Guideline
| Prefix | Comments | 
|---|---|
| ENH: | Enhancement, new functionality | 
| BUG: | Bug fix | 
| DOC: | Additions/updates to documentation | 
| BLD: | Updates to the build process/scripts | 
| PERF: | Performance improvement | 
| TEST: | Additions/updates to tests | 
| TYP: | Type annotations | 
| CLN: | Code cleanup, refactoring | 
| WIP: | checkpoint | 
| DEPR | deprecation | 
直前のCommit Messageの修正
test.txtの修正後, Commit Guidelineに従って, 「DOC: updating TOC of test.txt」と入力するところ,
誤って以下のようなcommit messageを入力してしまったとします
1
% git commit -m "DOCS: updating test.txt" 
git log (独自に定義したgit summaryコマンドをここでは使用, 詳しくはsee Appendix)で確認してみると
1
2
3
% git summary
2020-06-21T18:50:57+09:00,1d8d1bd,your-email-address,DOCS: updating test.txt
...
この直前のcommit messageを修正したい場合は, git commit --amendを使用します.
1
2
3
% git commit --amend -m "DOC: updating TOC of test.txt"
% git summary
2020-06-21T18:58:45+09:00,1daf2a2,your-email-address,DOC: updating TOC of test.txt
▶ REMARKS
- 修正前後のcommit-hashが1d8d1bd,1daf2a2と異なっていることに注意
- 作業が全てlocalで閉じられる時は問題はないが,すでにremoteへpushしてしまっている場合は非推奨
- 一度remoteへpushしてしまっている場合は,--forceでpushする必要がある
直前のCommit内容にミスが有り修正したい場合
1
2
% git add eda.py
% git commit -m "EDA-phase-1-task-1: histogram on the annual sales amount"
上記のようにeda.pyを編集 & commitした直後に,フォーマッターをかけ忘れていたことに気付き修正したいケースを考えます.
このとき,修正後に再度staging → git commit --amend --no-editとすることで新たにステージングされたファイルの変更履歴は
直前のcommitに含まれるようになります.
警告 ! 
- Remote repositoryにpusshされたcommitの修正となるようなケースは避けること
- あくまでローカルのみに存在するcommitを修正する程度の運用に留めること
過去複数のCommit Messageを修正したい場合
複数のcommitや任意の過去の時点のcommitのmessageを変更したい場合は, git rebaseコマンドを用います.
まず, 変更したいcommitを確認します:
1
2
3
4
% git summary
2020-06-21T22:35:24+09:00,dd929d9,foohoo@hoge.com,DOC: mv the 20230621/README to README
2020-06-21T22:35:20+09:00,3d188af,foohoo@hoge.com,DOC/WIP: adding a newline to README
2020-06-21T22:35:08+09:00,6a52472,foohoo@hoge.com,DOC/WIP: adding a newline to README
commit-hashが3d188af, 6a52472のprefixがDOC/WIPになっているので, これをDOCへ変更します.
直近の3つのcommitを表示して, その内上記の2つを変更していきます
このとき, git rebase -i HEAD~Nコマンドを使用します.
このコマンドはHEADからNまでのcommitを修正するという意味です. 今回は以下のように実行します:
1
% git rebase -i HEAD~3
Then, 次のようなリストが表示されます.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
pick 6a52472 DOC/WIP: adding a newline to README
pick 3d188af DOC/WIP: adding a newline to README
pick dd929d9 DOC: mv the 20230621/README to README
# Rebase b07cecb..dd929d9 onto b07cecb (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case
#                    keep only this commit's message; -c is same as -C but
#                    opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
#         create a merge commit using the original merge commit's
#         message (or the oneline, if no original merge commit was
#         specified); use -c <commit> to reword the commit message
# u, update-ref <ref> = track a placeholder for the <ref> to be updated
#                       to this position in the new commits. The <ref> is
#                       updated at the end of the rebase
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
次に, 3d188af, 6a52472のcommitをpickからrewordへ変更し, 保存して閉じます. つまり,
1
2
3
reword 6a52472 DOC/WIP: adding a newline to README
reword 3d188af DOC/WIP: adding a newline to README
pick dd929d9 DOC: mv the 20230621/README to README
すると, それぞれのcommitに対応するCOMMIT_EDITMSGがでてくるので修正して保存します.
commit messageが修正されているかどうか確認すると,
1
2
3
4
% git summary
2020-06-21T22:42:47+09:00,b81254f,foohoo@hoge.com,DOC: mv the 20230621/README to README
2020-06-21T22:42:43+09:00,3041854,foohoo@hoge.com,DOC: adding a newline to README
2020-06-21T22:41:53+09:00,2159f27,foohoo@hoge.com,DOC: adding a newline to README
以上のように変更されていました.
なお, 変更していないはずのdd929d9についてもcommit-hashがb81254fに書き換えられています.
これはrebaseの対象となってしまったので, その変更影響を受けてしまったことが理由になります.
Appendix: git summaryコマンド
git summaryは元々登録されているコマンドではなく, .gitconfigで自分が定義したコマンドです.
挙動としては以下と同じです:
1
2
3
% git log \
  --pretty=format:'%Cgreen%cI%Creset,%Cred%h%Creset,%C(bold blue)%ae%Creset,%s'\
  --abbrev-commit --decorate
~/.gitconfigにおける登録例は以下です:
1
2
[alias]
	summary = "log --pretty=format:'%Cgreen%cI%Creset,%Cred%h%Creset,%C(bold blue)%ae%Creset,%s' --abbrev-commit --decorate"
References
(注意:GitHub Accountが必要となります)