【Git】Git ~ 入門編 ~

■ 動画

Udacity の Freeコースを見つけたので、メモ。

Version Control with Git
https://www.udacity.com/course/version-control-with-git--ud123

■ Gitのファイル管理状態

【1】ローカル

1-1) ワーク ツリー (Work tree)
1-2) ステージング (Staging)
1-3) ローカル リポジトリ (Local repository)

【2】リモート

2-1) リモート リポジトリ (Remote repository)

状態遷移

* ワーク ツリー (Work tree)

 ↓ git add 

* ステージング (Staging)

 ↓ git commit 

* ローカル リポジトリ (Local repository)

 ↓ git push 

* リモート リポジトリ (Remote repository)

参考文献

動画
https://www.youtube.com/watch?v=i1L3A0SLDyg

 ■ 基本コマンド

 git init

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

構文

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

git init

共有リポジトリ設定

git init --bare

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

git add

 * ステージング・エリアに変更済みファイルを追加

構文

git add [ファイル]

git add hello.txt

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

git add .

 git commit

 * コミット

構文

git commit ([オプション])

git commit

コメント文「This is a Comment...」を追加

git commit -m "This is a Comment..."

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

git commit --amend

 git status

 * 変更の状態を確認する

構文

git status [オプション]

 git diff

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

git diff

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

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

 git cherry-pick

 * あるコミットの内容を任意のブランチに取り込む
 * cherry-pick:自分の気に入ったものだけをつまみ食いする, 〔品物などを〕入念に[用心深く]選ぶ
etc

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

 ■ サンプル

 * Hello World的なこととして、ファイルをgitで
   バージョン管理してみる

 準備

mkdir my-web
cd my-web
vi index.html
~~~~~~~
Hello World
~~~~~~~

 コマンド例:一からGitリポジトリを作るところから

初期化:git init

# 現在のディレクトリ(今回は「my-web」)を
# gitで使うためのコマンド
git init
 => 「Initialized empty Git repository in /home/【ユーザ名】/my-web/.git/」

ステージングに移行:git add

# 「作業ディレクトリ」→「ステージング・エリア(インデックス)」に遷移するためのコマンド
git add index.html

レポジトリに移行:git commit

# 「ステージング・エリア(インデックス)」→「レポジトリ(Gitディレクトリ)」に遷移するためのコマンド
git commit

ログ確認:git log

# 確認
git log

状態確認:git status

vi index.html
~~~~~~~
Hello World!!
~~~~~~~

# 変更確認
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:   index.html

no changes added to commit (use "git add" and/or "git commit -a")

元に戻す:git checkout

# 元に戻す
git checkout -- index.html

# 再度、変更確認
git status

On branch master
nothing to commit, working tree clean

変更確認:git diff

vi index.html
~~~~~~~
Hello World!!
~~~~~~~

# 変更確認
git diff

diff --git a/index.html b/index.html
index 557db03..9369771 100644
--- a/index.html
+++ b/index.html
@@ -1 +1 @@
-Hello World
+Hello World!!

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

# ステージング・エリアに遷移
git add index.html

# 変更確認 (既にステージング・エリアに遷移しているので、出力結果は何も表示されない)
git diff

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

diff --git a/index.html b/index.html
index 557db03..9369771 100644
--- a/index.html
+++ b/index.html
@@ -1 +1 @@
-Hello World
+Hello World!!

 ■ 使用上の注意

ファイルの削除/移動について

 * ファイルの削除、移動する際には、gitコマンドを使う

Gitコマンド ~ mv 移動 / rm 削除 ~
https://dk521123.hatenablog.com/entry/2019/12/07/234428

git mv index.html ./hello/index.html

git rm index.html

参考文献

https://qiita.com/hshimo/items/ab91b99cd61724127aa7
https://qiita.com/usizou/items/9e8ca22511b6d4e5a574

関連記事

Git

Git ~ 初期設定 / Windows 編 ~
https://dk521123.hatenablog.com/entry/37686059
Git ~ 初期設定 / Linux 編 ~
https://dk521123.hatenablog.com/entry/37613741
Git ~ 基本編 / ブランチ ~
https://dk521123.hatenablog.com/entry/2018/06/30/135023
Git ~ ブランチあれこれ ~
https://dk521123.hatenablog.com/entry/2022/07/10/000000
Git ~ 基本編 / マージ・リベース ~
https://dk521123.hatenablog.com/entry/2019/06/10/200917
Git ~ 基本編 / 共有リポジトリ
https://dk521123.hatenablog.com/entry/2018/06/27/084600
Git ~ 基本編 / 基本コマンド ~
https://dk521123.hatenablog.com/entry/2020/10/02/000000
Git ~ 基本編 / あれこれ ~
https://dk521123.hatenablog.com/entry/2018/07/01/005431
Git ~ git config あれこれ ~
https://dk521123.hatenablog.com/entry/2021/01/10/000000
Git ~ 改行コード自動変換 core.autocrlf ~
https://dk521123.hatenablog.com/entry/2021/08/17/171756
Gitコマンド ~ mv 移動 / rm 削除 ~
https://dk521123.hatenablog.com/entry/2019/12/07/234428
Git ~ バックアップ / Windows 編 ~
https://dk521123.hatenablog.com/entry/37687573
【Git】 状態を戻す方法を考える
https://dk521123.hatenablog.com/entry/2020/07/11/182959
【Git】 状態を戻す際に必要な文法・コマンド ~ reset / revert etc ~ https://dk521123.hatenablog.com/entry/2020/07/12/103726
Githubで状態を戻す方法を考える
https://dk521123.hatenablog.com/entry/2022/08/03/000000
Git ~ 過去ソースの取得方法 ~
https://dk521123.hatenablog.com/entry/2023/08/23/133313
Git クライアントソフト
https://dk521123.hatenablog.com/entry/2019/09/27/153937

Git/Githubトラブル

Windows上でGitの認証が通らなくなった
https://dk521123.hatenablog.com/entry/2019/12/21/222431
Git 移行時におけるトラブル
https://dk521123.hatenablog.com/entry/2022/02/02/000000

Git / Web UI

Git ~ Web UI編 ~
https://dk521123.hatenablog.com/entry/37634122
Git ~ Web UI / GitLab 編 ~
https://dk521123.hatenablog.com/entry/37722002
Git ~ Web UI / Ginatra 編 ~
https://dk521123.hatenablog.com/entry/37701141
GitBucket ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/09/24/201222
GitBucket ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2019/09/25/214415

その他

多要素認証(MFA:Multi-Factor Authentication)
https://dk521123.hatenablog.com/entry/2019/10/03/222600
Githubssh 接続するには...
https://dk521123.hatenablog.com/entry/2019/10/17/205947