【Git】Git ~ 基本編 / 基本コマンド ~

■ はじめに

Gitクライアントソフトが使えなくなったので、
仕方がないから、一連の作業をコマンドラインで行う

目次

【1】基本コマンド
 1)git clone コマンド
 2)git init コマンド
 3)git add コマンド
 4)git commit コマンド
 5)git push コマンド
 6)git mv コマンド
 7)git rm コマンド
 8)git status コマンド
 9)git diff コマンド
 10)git fetch コマンド
 11)git log コマンド
 12)git cherry-pick コマンド
【2】サンプル
 例1:ソースダウンロード~コミットまで

【1】基本コマンド

1)git cloneコマンド

ソースのダウンロードする

 2)git initコマンド

* リポジトリを新規作成するときに使うコマンド

構文

git init ([ディレクトリ] [オプション])

git init

共有リポジトリ設定

git init --bare

参考文献 https://eng-entrance.com/git-init

3)git addコマンド

* ステージング・エリアにファイル追加
 => 「作業ディレクトリ」から「ステージング・エリア(インデックス)」
  に遷移するためのコマンド

構文

# git add <your-file>
git add README.md

現在のディレクトリ配下のもの全て追加

git add .

 4)git commitコマンド

* コミット
 => 「ステージング・エリア(インデックス)」から
  「レポジトリ(Gitディレクトリ)」に遷移するためのコマンド

構文

git commit ([オプション])

git commit -m "コメント"

git commit

# コメント文「This is a Comment...」を追加
git commit -m "This is a Comment..."

直前のコミットを変更する

git commit --amend

5)git pushコマンド

* プッシュする

構文

git push origin <ブランチ名>

6)git mvコマンド

* 移動する
 => ファイルのリネームにも使える
* 詳細は以下の関連記事を参照のこと

https://dk521123.hatenablog.com/entry/2019/12/07/234428

7)git rmコマンド

* 削除する
* 詳細は以下の関連記事を参照のこと

https://dk521123.hatenablog.com/entry/2019/12/07/234428

 8)git statusコマンド

* 変更の状態を確認する

構文

git status [オプション]

 9)git diffコマンド

* ステージング・エリアにないファイルの変更状態を確認する

構文

git diff [<options>] [--] [<path>]
git diff [<options>] <commit><commit> [] [<path>]

https://tracpath.com/docs/git-diff/
https://git-scm.com/docs/git-diff

#  [インデックス] → [作業ツリー] の差分を見る
git diff

# 次の commit で反映される変更を表示
git diff --cached

# リモートブランチ「develop」とブランチ「origin/develop feature/demo1」との比較
git diff origin/develop feature/demo1

# リモートブランチ「develop」と自分のブランチとの比較
# --name-only: 変更されたファイルの名前のみを表示
# HEAD: 現在のブランチの先頭のコミットへの参照
# -- "sqls/*.sql" : sqls配下のSQLファイルのみ抽出
git diff --name-only origin/develop HEAD -- "sqls/*.sql"

# ..: コミット同士の差分(ベースブランチの変更が含まれる)
# ...: 分岐からの差分(ベースブランチの変更が含まれない)
git diff --name-only origin/develop...origin/feature/demo1

参考文献
http://www-creators.com/archives/755

10)git fetchコマンド

* リモートレポジトリからブランチ(とタグ)の情報を
 ローカルレポジトリに取得する

cf. fetch (フェッチ) = とってくる、引き戻す、連れ戻す

構文

git fetch [リポジトリ]

git fetch <リモート名> <リモートブランチ名>

https://tracpath.com/docs/git-fetch/

# origin にあるのすべてのブランチを取得する
git fetch origin

# origin から master ブランチに関する履歴と参照を取得する
git fetch origin master

git fetch origin develop:develop

pullコマンドとの違い

* git pullコマンドの方は使用すると、git fetchとgit mergeが動く
 => 最新のソースコードが変わる
 => git fetchコマンドは最新のソースコードの状況を確認したり、
  最新のブランチを取得したいときなどに使用する

11)git logコマンド

* コミット履歴の閲覧

Git - コミット履歴の閲覧

 12)git cherry-pickコマンド

* あるコミットの内容を任意のブランチに取り込む

cf cherry-pick:自分の気に入ったものだけをつまみ食いする, 
 〔品物などを〕入念に[用心深く]選ぶ

https://eow.alc.co.jp/search?q=cherry-pick
構文

git cherry-pick [コミットID]

# ID「9402b408ad7f5315a9757241fec5c7ebb2afb7c4」の内容を
# ブランチに取り込む(git logで調べられる)
git cherry-pick 9402b408ad7f5315a9757241fec5c7ebb2afb7c4  

参考文献
https://qiita.com/bossunn24/items/dedb620541d852327934
https://qiita.com/ta__ho/items/8204a22a53b02ee0817e
https://qiita.com/shiren/items/737f8974c3bdd9018d58

【2】サンプル

例1:ソースダウンロード~コミットまで

# Step1 : Githubにあるブランチ「develop」をダウンロードする
git clone https://github.com/xxxx/xxxx.git -b develop

# ソースを修正 ...

# Step2 : 修正した点を確認
git diff

# Step3 : 変更点をステージングへ移行
git add .

# Step4 : コミット
git commit -m "Add your comment"

# Step5 : プッシュ
git push origin develop

参考文献

https://qiita.com/infratoweb/items/5021a36f69f26dc7f0b9
https://qiita.com/konweb/items/621722f67fdd8f86a017

関連記事

Git ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2018/06/29/104028
Git ~ 基本編 / あれこれ ~
https://dk521123.hatenablog.com/entry/2018/07/01/005431
状態を戻す際に必要な文法・コマンド ~ reset / revert etc ~
https://dk521123.hatenablog.com/entry/2020/07/12/103726
Git クライアントソフト
https://dk521123.hatenablog.com/entry/2019/09/27/153937