Skip to content

Instantly share code, notes, and snippets.

@kurotaky
Last active April 10, 2022 05:19
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kurotaky/5483492 to your computer and use it in GitHub Desktop.
Save kurotaky/5483492 to your computer and use it in GitHub Desktop.
git commit するまでに自分がやっていること

コミットするまでの流れ

前回のコミットから何も変更を加えていない状態。

$ git status
# On branch masternothing to commit, working directory clean

エディタでファイルを編集する

$ vim app/views/hello/list.html

編集後、git statusで変更部分を確認。

$ git status

# On branch master
# Changes not staged for commit:#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)#
#       modified:   app/views/hello/list.html
#
no changes added to commit (use "git add" and/or "git commit -a")

差分を確認

$ git diff

今回はCD-ROMの項目を削除した、ということを確認できる。

diff --git a/app/views/hello/list.html b/app/views/hello/list.htmlindex a0c13ea..b6de0e7 100644
--- a/app/views/hello/list.html
+++ b/app/views/hello/list.html
@@ -1,7 +1,7 @@
 <table border="1">
   <tr>
     <th>ISBNコード</th><th>書名</th><th>価格</th>
-    <th>出版社</th><th>刊行日</th><th>CD-ROM</th>
+    <th>出版社</th><th>刊行日</th>
   </tr>

   <% @books.each do |book| %>
@@ -11,7 +11,6 @@
     <td><%= book.price %>円</td>
     <td><%= book.publish %></td>
     <td><%= book.published %></td>
-    <td><%= book.cd %></td>
   </tr>
   <% end %>
 </table>

git add してindexに登録する。

$ git add app/views/hello/list.html


# -p オプションをつけると細かく add できる。
# y(yes), n(no) でaddするかどうか選択できる。
# s でさらに細かくできる
$ git add -p app/views/entries/edit.html.haml

git statusで確認

$ git status

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   app/views/hello/list.html
#

自分がインデックスに追加したものが意図しているものかどうか、 git diff --cached でコミット前に差分を確認する

$ git diff --cached
diff --git a/app/views/hello/list.html b/app/views/hello/list.html
index a0c13ea..b6de0e7 100644
--- a/app/views/hello/list.html
+++ b/app/views/hello/list.html
@@ -1,7 +1,7 @@
 <table border="1">
   <tr>
     <th>ISBNコード</th><th>書名</th><th>価格</th>
-    <th>出版社</th><th>刊行日</th><th>CD-ROM</th>
+    <th>出版社</th><th>刊行日</th>
   </tr>

   <% @books.each do |book| %>
@@ -11,7 +11,6 @@
     <td><%= book.price %>円</td>
     <td><%= book.publish %></td>
     <td><%= book.published %></td>
-    <td><%= book.cd %></td>
   </tr>
   <% end %>
 </table>

コミットする(-v をつけるとコミットメッセージを書く時に差分が表示される)

$ git commit -v

CD-ROM付属の雑誌を扱わなくなったため、CD-ROMの項目を削除した

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   app/views/hello/list.html
#
diff --git a/app/views/hello/list.html b/app/views/hello/list.html
index a0c13ea..b6de0e7 100644
--- a/app/views/hello/list.html
+++ b/app/views/hello/list.html
@@ -1,7 +1,7 @@
 <table border="1">
   <tr>
     <th>ISBNコード</th><th>書名</th><th>価格</th>
-    <th>出版社</th><th>刊行日</th><th>CD-ROM</th>
+    <th>出版社</th><th>刊行日</th>
   </tr>

   <% @books.each do |book| %>
@@ -11,7 +11,6 @@
     <td><%= book.price %>円</td>
     <td><%= book.publish %></td>
     <td><%= book.published %></td>
-    <td><%= book.cd %></td>
   </tr>
   <% end %>
 </table>

コミットが新しく追加されているか確認

$ git log

tigを使う(macでhomebrewを使っている場合 brew install tig でインストールできる)

$ tig  

git commit するまでに気をつけていること

  • 未来の自分、自分以外の人が今からcommitする内容を見て理解できるか?理解するのに時間がかからないか?
  • コミットの粒度は適切?(typoの修正と機能の追加のコミットは分ける)
  • コミットメッセージは適切か?(誰が、何を、なぜしたか)
  • 無駄なゴミファイルやtypoは無い?

その他

gitconfig

一回一回 git diff --cached とか打つのはだるいので .gitconfigを編集してタイプ数を減らす。

$ vim ~/.gitconfig

自分が使いやすいように設定する。

[alias]
    st   = status
    ci   = commit
    dc   = diff --cached

git 資料

Web

入門
コミットの仕方

他にもたくさんあります。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment