【Git】Git ~ 入門編 ~

■ はじめに

Git に関するはじめの一歩について記す

目次

【1】動画
【2】Gitのファイル管理状態
 1)状態遷移
【3】基本コマンド
【4】サンプル
 1)準備
 2)コマンド例:一からGitリポジトリを作るところから
【5】使用上の注意
 1)ファイルの削除/移動について
 2)例

【1】動画

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

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

【2】Gitのファイル管理状態

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

[a] ローカル側

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

[b] リモート側

[1] リモート リポジトリ (Remote repository)

1)状態遷移

* ワーク ツリー (Work tree)
 ↓ git add 
* ステージング (Staging)
 ↓ git commit 
* ローカル リポジトリ (Local repository)
 ↓ git push 
==[リモート]====================
 ↓ 
* リモート リポジトリ (Remote repository)

 【3】基本コマンド

* 以下の関連記事を参照のこと

Git ~ 基本編 / 基本コマンド ~
https://dk521123.hatenablog.com/entry/2020/10/02/000000

 【4】サンプル

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

 1)準備

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

 2)コマンド例:一から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!!

 【5】使用上の注意

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

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

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

2)例

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 ~ git remote prune ~
https://dk521123.hatenablog.com/entry/2024/03/19/193556
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
PC が新しくなった際のGit/Githubの設定
https://dk521123.hatenablog.com/entry/2024/01/06/000000

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