git commitの基本的な使用方法とは?コミットをまとめる方法や取り消す方法についても紹介!

git commitの使い方とは?
今回は、git commitの使い方について説明します。
ソース管理にgitを使用することは一般的です。git commitコマンドは、ファイルの修正をgitリポジトリに保存します。
この記事では、基本的な使い方から、コミットをまとめる方法や取り消す方法について紹介します。
git commitの使い方に興味のある方はぜひご覧ください。
基本的な使い方
git commitの基本的な使い方を紹介します。ここでは、gitのバージョンは以下となります。
1
2
|
~$ git --version
git version 2.17.1
|
初回コミットのためのファイルを作成して、git addコマンドで追加します。
1
2
|
~/$ echo "# sample" >> README.md
~/$ git add README.md
|
git statusコマンドで状態を確認します。当然ですが、まだコミットしていない状態です。
1
2
3
4
5
6
7
8
9
|
~/$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md
|
git commitコマンドでコミットします。-mオプションでコミットコメントを記述するのが一般的です。
1
2
3
4
|
~/$ git commit -m "first commit"
[master (root-commit) 749880e] first commit
1 file changed, 1 insertion(+)
create mode 100644 README.md
|
git logコマンドで履歴を確認します。-mで指定したコメントも表示されます。
1
2
3
4
5
6
|
~/$ git log
commit 749880e1c2642eb829555c7af290d08c3cdf554a (HEAD -> master)
Author: ${GitHubユーザ名} <${GitHubメールアドレス}>
Date: Fri Jan 22 23:14:13 2021 +0000
first commit
|
最後にgit pushコマンドでリモートにpushします。
1
|
~/$ git push
|
直前のコミットメッセージの変更
直前のコミットメッセージを変更する方法を紹介します。git commitコマンドに「–amend」オプションを指定します。
1
|
~/$ git commit --amend
|
以下のような、コミット情報が表示されます。メッセージを修正して保存してください。ここでは「first commit. add README.md」のように修正しました。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
first commit
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Fri Jan 22 23:14:13 2021 +0000
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
# new file: README.md
#
|
修正後にgit logを実行してみると、コミットコメントが修正されていることが分かります。
1
2
3
4
5
6
7
|
~/$ git log
commit 4ed57fec9113f4fdc6952a76a5b781fc74a8ad35 (HEAD -> master)
Author: ${GitHubユーザ名} <${GitHubメールアドレス}>
Date: Fri Jan 22 23:14:13 2021 +0000
first commit. add README.md
~/$
|
コミットをまとめる
こまめにコミットしすぎると、履歴が汚くなります。ここでは、複数のコミットをまとめる方法を紹介します。git logコマンドで履歴を確認します。
1
2
3
4
5
|
~/$ git log --oneline
4f2a560 (HEAD -> master, origin/master) modify3
f805106 modify2
76df526 modify1
0972003 Merge remote-tracking branch 'origin/master'
|
コミットをまとめるには、git rebaseコマンドを使用します。ここでは、modify1、modify2、modify3のコミットをまとめたいとします。そのため、modify1の一つ前のhash値を指定します。
1
|
~/$ git rebase -i 0972003
|
実行すると、以下のような表示に切り替わります。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
pick 76df526 modify1
pick f805106 modify2
pick 4f2a560 modify3
# Rebase 0972003..4f2a560 onto 0972003 (3 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# 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.
#
# Note that empty commits are commented out
|
今回は、modify2およびmodify3の指示コマンドを(squash)に修正します。squashは、直前のpickを指定したコミットに統合し、メッセージも統合する指示コマンドです。
1
2
3
|
pick 76df526 modify1
s f805106 modify2
s 4f2a560 modify3
|
保存すると、コミットメッセージ編集画面に切り替わります。自由にコミットメッセージを編集してください。
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
|
# This is a combination of 3 commits.
# This is the 1st commit message:
modify1
# This is the commit message #2:
modify2
# This is the commit message #3:
modify3
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Fri Jan 22 23:38:38 2021 +0000
#
# interactive rebase in progress; onto 0972003
# Last commands done (3 commands done):
# squash f805106 modify2
# squash 4f2a560 modify3
# No commands remaining.
# You are currently rebasing branch 'master' on '0972003'.
#
# Changes to be committed:
# modified: sample1.txt
#
|
保存後、originのcommitを破棄して、強制pushします。-fオプションを使用しますが、安易に強制pushしないように注意してください。
1
|
~/$ git push -f origin master
|
git logコマンドで履歴を確認してみます。
1
2
3
4
5
6
7
8
9
10
|
~/$ git log
commit ea0112b1849625c27dc620c97241e1cdc13a66bb (origin/master)
Author: ${GitHubユーザ名} <${GitHubメールアドレス}>
Date: Fri Jan 22 23:38:38 2021 +0000
modify1
modify2
modify3
|
コミットが1つにまとめられていることが分かります。
直前のコミットを取り消す
直前のコミットを取り消す方法を紹介します。git resetコマンドを使用します。
直前のコミットだけを取り消すには、–softオプションを指定します。
1
|
~/$ git reset --soft HEAD^
|
–softオプションは、インデックスと作業ツリーはそのままにします。git statusコマンドで状態を確認すると、未コミットの状態が確認できます。
1
2
3
4
5
6
7
8
9
10
11
|
~/$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: sample1.txt
~/$
|
インデックスと作業ツリーも含めて、直前のコミット直後の状態に戻すには、–hardオプションを指定します。
1
|
~/$ git reset --hard HEAD^
|
git statusコマンドで状態を確認すると、未コミットのものがない状態が確認できます。
1
2
3
4
5
6
7
|
~/$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
~/$
|
まとめ
いかがでしたでしょうか。git commitの基本的な使い方から、コミットをまとめる方法や取り消す方法について紹介しました。
ぜひご自身でコマンドを書いて、理解を深めてください。
ネプラス株式会社はサービス開始から10年以上
『エンジニアの生涯価値の向上』をミッションに掲げ、
多くのインフラエンジニア・ネットワークエンジニアの就業を支援してきました。
ネプラス株式会社はこんな会社です
秋葉原オフィスにはネプラス株式会社をはじめグループのIT企業が集結!
数多くのエンジニアが集まります。

-
インフラ業界に特化
ネットワーク・サーバー・データベース等、ITインフラ業界に特化。Cisco Systemsプレミアパートナーをはじめ各種ベンダーのパートナー企業です。
業界を知り尽くしているからこそ大手の取引先企業、経験豊富なエンジニアに選ばれています。
-
正社員なのにフリーランスのような働き方
正社員の方でも希望を聞いたうえでプロジェクトをアサインさせていただいており、フリーランスのような働き方が可能。帰社日もありません。
プロジェクト終了後もすぐに次の案件をご紹介させていただきますのでご安心ください。
-
大手直取引の高額案件
案件のほとんどが大手SIerやエンドユーザーからの直取引のためエンジニアの皆様へに高く還元できています。
Ciscoをはじめ、Juniper、Azure、Linux、AWS等インフラに特化した常時300件以上の案件があります。
-
スキルアップ支援
不要なコストを削減し、その分エンジニアの方へのスキルアップ支援(ネットワーク機器貸出、合格時の受験費用支給など)や給与で還元しています。
受験費用例)CCNP,CCIE:6-20万円、JNCIS:3-4万円、AWS:1-3万円など
※業務に関連する一定の資格のみ。各種条件がありますので詳しくは担当者へにお尋ねください。
-
現給与を保証します!※
前職の給与保証しており、昨年度は100%の方が給与アップを実現。収入面の不安がある方でも安心して入社していただけます。
※適用にはインフラエンジニアの業務経験1年以上、等一定の条件がございます。
-
インセンティブ制度
ネットワーク機器の販売・レンタル事業等、売上に貢献いただいた方にはインセンティブをお支払いしています。
取引先企業とエンジニア側、双方にメリットがあり大変好評をいただいています。
-
社会保険・福利厚生
社員の方は、社会保険を完備。健康保険は業界内で最も評価の高い「関東ITソフトウェア健康保険組合」です。
さらに様々なサービスをお得に利用できるベネフィットステーションにも加入いただきます。
-
東証プライム上場企業グループ
ネプラスは東証プライム上場「株式会社オープンアップグループ」のグループ企業です。
安定した経営基盤とグループ間のスムーズな連携でコロナ禍でも安定した雇用を実現させています。
ネプラス株式会社に興味を持った方へ
ネプラス株式会社では、インフラエンジニアを募集しています。
年収をアップしたい!スキルアップしたい!大手の上流案件にチャレンジしたい!
オンライン面接も随時受付中。ぜひお気軽にご応募ください。

