LoginSignup
0
0

More than 1 year has passed since last update.

Gitの基本的なコマンド操作(git init~git commitまでの詳細説明)

Last updated at Posted at 2021-06-20

説明

  • 前回はコマンド一覧のみを書きましたが今回から具体的な使い方の説明をしていきます。
  • 本記事は初学者向けとド忘れしたときの手助けになればと書いています。

git init ->リポジトリの初期化

作成したディレクトリまで移動しgit initコマンドを叩く下記の一文が出ていれば成功です。
「Initialized empty Git repository in /Users/Desktop/git/.git/」
これでgitでバージョン管理する準備ができました。

% git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint:   git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint:   git branch -m <name>
Initialized empty Git repository in /Users/Desktop/git/.git/

git status ->リポジトリの状態確認

初期化すぐに「git status」を行うと図1の画面になり何もない事が表示されます。
新しくファイルを追加して再度、「git status」を行うと図2のようになります。
このように状態を確認できるコマンドです。

図1

% git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

図2

% git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    README.md

nothing added to commit but untracked files present (use "git add" to track)

git commit ->リポジトリの歴史を記録(詳細なコミットメッセージを記述できる)

「git add」コマンドでステージングさせ「git commit」を行うとエディタが表示され下記のように表示されます。
記述できるようにするにはキーボードの「I」を押し「-- INSERT --」と表示されれば記述できるようになります。
終わればキーボードの「esc」を押し「:w」で保存して「:q」で終わるとコミットが行われます。
うまく行くと図2のようになります。

図1

ここにコミットメッセージを記述
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch feature-D
# Your branch is up to date with 'origin/feature-D'.
#
# Changes to be committed:
#       modified:   README.md
#
~  ここから下に変更した理由や詳細を記述                                                                             
~
~
~
~
~
~
-- INSERT --

図2

[feature-D 8136cbd] Edit
 1 file changed, 2 insertions(+), 2 deletions(-)

git commit -m "コミットメッセージを記述する" ->リポジトリの歴史を記録(1行のコミットメッセージを記述する)

こちらも「git add」コマンドを行った後に「git commit -m」を行います。
「git commit -m」はダブルクオーテーションの中にコミットメッセージを記述して行います。
下記の様になれば成功です。

% git commit -m "Add index"
[master 4ae8ec7] Add index
 1 file changed, 1 insertion(+)

git commit -am "コミットメッセージを記述する" ->git addとgit commit -mを同時に行う

「git commit -am」は「git add」と「git commit -m」を一回のコマンドで行えます。
使い方は「git commit -m」と同じでダブルクオーテーションの中にコミットメッセージを記述して行います。
下記の様になれば成功です。

 % git commit -am "Add feature-C"
[feature-C cfaf6ef] Add feature-C
 1 file changed, 1 insertion(+)

git commit --amend ->コミットメッセージを修正

「git commit --amend」を行うと下記の画面のようにエディタが立ち上がります。
※エディタの操作については簡単ではありますが上記の方の「git commit」の所で説明しているのでわからない場合は参考にしてください。
コミットメッセージが書かれている所を消して再度、記述します。
詳細も必要に応じて書き保存して終了すると図2の様になります。

図1

Edit ←これがコミットメッセージ

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sun Jun 20 13:53:16 2021 +0900
#
# On branch feature-D
# Your branch is ahead of 'origin/feature-D' by 1 commit.
#   (use "git push" to publish your local commits)
#
# Changes to be committed:
#       modified:   README.md
#
~    ここから下に詳細などがコミット時に書かれていた場合その詳細も表示されます                                                                           
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
-- INSERT --

図2

% git commit --amend
[feature-D f64802f] Amend
 Date: Sun Jun 20 13:53:16 2021 +0900
 1 file changed, 2 insertions(+), 2 deletions(-)

今回はここまで

簡単ではあるのですが僕が学習していてつまづいたりこうやって書いてくれているとありがたいという気持ちで書いたので初学者や慣れ始めた方達の助けになればと思っています。

他のコマンドもまた書いていきたいと思っています。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0