LoginSignup
33
42

More than 1 year has passed since last update.

Gitコマンド 最速 実践練習フロー【2018.11】

Last updated at Posted at 2018-11-24

背景

  • 分かりづらく長くなりがちなGitの解説が多かったので、最小限の言葉で
    とにかく直ぐにGitを実践し体得できるように作成
  • 最速で、Gitのコア/要点/イメージが直ぐに掴めるようなコマンドで構成
  • 分かりやすく噛み砕いた用語で記載
  • ネット上のGit解説は誤った情報もあり、正確な内容をよく知るため かなり精査

環境

Remort Repository : Github
MyPC OS : mac os High Sierra
git version : 2.17.1 ( Apple Git-112, gitインストール済み )

重要用語

  1. Working ( ワーキング, Working Area, ワーキングエリア, 通常作業フォルダ )
    → Staging に登録( add )されるファイルフォルダ

  2. Staging ( ステージング, Staging Area, ステージングエリア, Index Area, インデックスエリア, Index, インデックス )
    → Local Repository に登録( commit )されるファイルフォルダ

  3. Local Repository ( ローカルリポジトリ, Commit Area, コミットエリア )
    → Remort Repository に登録( push )されるファイルフォルダ

  4. Remort Repository ( リモートリポジトリ )
    → GitHub, GitLab などのクラウド上に保管/共有されるファイルフォルダ

  5. Commit ( コミット )
    → Repositoryに保存されるセーブポイント。ゲームと同じイメージで 戻りたい時に戻れるデータ。

  6. Branch ( ブランチ )
    → バージョン / パターン

  7. HEAD ( ヘッド )
    → 現在作業中のブランチ

その他ポイント

練習 1 - 初回

Gitインストール 〜 GitHub 〜 Remote Repository への Commit まで、確認しながら丁寧に。

1. Gitインストール

$ git --version でバージョンが表示されたらOK。
-v や ver だけでは表示されない。
Screen Shot 2019-10-27 at 9.07.43.png

2. GitHub にて Remote Repository 作成

GitHubアカウント作成とリポジトリの作成手順 @kooohei - Qiita
上記 記事を参考に

3. ローカル環境 ( My PC )

###1 ターミナル起動
Screen Shot 2018-11-11 at 18.16.57.png

###2 pwd 
[ 現在いるフォルダ階層位置 確認 ( user はユーザーネームフォルダ ) ]

$ pwd
/Users/user

###3 ls 
[ フォルダ内ファイル確認 ]

$ ls
Applications		Downloads		Movies			cache.ga
Creative Cloud Files	Google Drive		Music			evalonly.txt
Desktop			Library			Pictures
Documents		Local Sites		Public

###4 mkdir 
[ 新規フォルダ作成 ( フォルダ名/作成場所 共に適当でOK ) ]

$ mkdir gittest01

###5 ls 
[ フォルダ内再確認 ( gittest01フォルダ作成を確認 ) ]

$ ls
Applications		Downloads		Movies			cache.ga
Creative Cloud Files	Google Drive		Music			evalonly.txt
Desktop			Library			Pictures		gittest01
Documents		Local Sites		Public

###6 cd 
[ 作成フォルダへ移動 ]

$ cd gittest01/

###7 git init 
[ Git Local Repository 作成 実行 ]

$ git init
Initialized empty Git repository in /Users/user/gittest01/.git/

###8 ls -a 
[ フォルダ内ファイル確認 ( .git 隠しフォルダが作成されたことを確認 ) ]

$ ls -a 
.	..	.git

###9 ls .git/ -a 
[ .gitフォルダ内ファイル確認 ( 7種類のファイル/フォルダが自動生成されたことを確認。この時点 index ファイルは未だ無い ) ]

$ ls .git/ -a
ls: -a: No such file or directory
.git/:
HEAD		description		objects
config		hooks		info		refs

###10 練習用に適当なテキストファイルを作成保存 / ls で確認

$ touch gittest01_text.txt 

$ ls
gittest01_text.txt

###11 git add --dry-run . 
[ Working ファイル確認 ( これから Staging に登録されるファイル ) ]

$ git add --dry-run . 
add 'gittest01_text.txt'

###12 git ls-files 
[ Staging ファイル確認 ( 勿論まだ何も登録されていない状態 ) ]

$ git ls-files
 

###13 git status 
[ Working と Staging ファイルの Commit 状態確認 ( Working に gittest01_text.txtファイルが Untracked ) ]

$ git status
On branch master

No commits yet

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

	gittest01_text.txt

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

###14 git add . 
[ Working ファイル全てを Staging へ ファイル登録( add ) 実行 ]

$ git add .

###15 git ls-files 
[ Staging ファイル確認 ( gittest01_text.txt が登録された ) ]

$ git ls-files
gittest01_text.txt 

###16 ls .git/ -a 
[ .gitフォルダ内確認 ( Staging 用の index ファイルが自動生成されたことを確認 ) ]

$ ls .git/ -a
ls: -a: No such file or directory
.git/:
HEAD		description		index	objects
config		hooks		info		refs

###17 git status 
[ Working と Staging ファイルの Commit 状態確認 ( Staging に登録され、Local Repository に Commit できる状態 ) ]

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

	new file:   gittest01_text.txt

###18 git diff --staged 
[ Staging と Local Repository の差分 確認 ( Staging にファイルが登録され、今度は Local Repository との差分がある状態 ) ]

$ git diff --staged
diff --git a/gittest01_text.txt b/gittest01_text.txt
new file mode 100644
index 0000000..27706f8
--- /dev/null
+++ b/gittest01_text.txt
@@ -0,0 +1 @@
+

###19 git commit -m "初めてのコミット" 
[ コメントを付けて Staging のファイルを Local Repository へ ファイル登録( commit ) 実行 ]

$ git commit -m "初めてのコミット"
[master (root-commit) 0938823] 初めてのコミット
 Committer: User <User@user-no-MacBook-Pro.local>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 gittest01_text.txt

下記 メッセージが表示され、メルアドとユーザーネーム登録を求められたら登録実行

Please tell me who you are.

$ git commit -m "初めてのコミット"

*** Please tell me who you are ~ 

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'user@usernombp.(none)')

#####19-1. git config --global user.email メールアドレス

#####19-2. git config --global user.name

#####19-3. git config --global --list

$ git config --global --list
fatal: unable to read config file '/Users/user/.gitconfig': No such file or directory

$ git config --global --add user.email *****+user@users.noreply.github.com

$ git config --global user.name "user"

$ git config --global --list
user.email=*****+user@users.noreply.github.com
user.name=user

$ vim ~/.gitconfig
[user]
        email = *****+user@users.noreply.github.com
        name = user

###20 git status  
[ Working と Staging ファイルの Commit 状態確認 ( Local Repository が最新であるため、何も問題が無い状態 ) ]

$ git status
On branch master
nothing to commit, working tree clean

###21 git diff --staged  
[ Staging と Local Repository の差分確認 ( 差分が無くなった ) ]

$ git diff --staged

###22 git show  
[ 最新 Commit 内容表示 ]

$ git show
commit 0938823094d05ddb762b39d8dae1563d8a2c2376 (HEAD -> master)
Author: user <user@user-no-MacBook-Pro.local>
Date:   Mon Nov 12 00:33:38 2018 +0800

    初めてのコミット

diff --git a/gittest01_text.txt b/gittest01_text.txt
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/gittest01_text.txt
@@ -0,0 +1 @@
+

###23 git log -1  
[ Commit 履歴 最新1個表示 ]

$ git log -1
commit 0938823094d05ddb762b39d8dae1563d8a2c2376 (HEAD -> master)
Author: user <user@user-no-MacBook-Pro.local>
Date:   Mon Nov 12 00:33:38 2018 +0800

    初めてのコミット

###24 git branch  
[ Local Branch 確認 ( master Branch が生成された ) ]

$ git branch
* master

###25 git reflog  
[ Branch切り替え確認 ( HEAD が master Branch となった ) ]

$ git reflog
0938823 (HEAD -> master) HEAD@{0}: commit (initial): 初めてのコミット

###26 git remote  
[ Remote Repository 登録確認 (未登録状態) ]

$ git remote 

###27 git remote add origin https://github.com/user/gittest01.git  
[ Remote Repository 登録 ]

$ git remote add origin https://github.com/user/gittest01.git

###28 git remote
[ Remote Repository Name 確認 ]

$ git remote 
origin

###29 git branch
[ branch Name 確認 ]

$ git branch
* master

###32 git push origin master 
[ Remote Repository に Local Repository を反映 ]

$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 256 bytes | 256.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/user/gittest01/pull/new/master
remote: 
To https://github.com/user/gittest01.git
 * [new branch]      master -> master

4. Github 確認

GitHub上でファイルの登録確認

練習1 - 初回 : Fin

練習 2: スピードアップ

新規ファイル作成 〜 Remote Repository への Commit まで。コマンドを絞り、初回の倍以上のスピードで。

###1 練習用に適当なテキストファイルを作成保存 / ls で確認

$ touch gittest02_text.txt 

$ ls
gittest01_text.txt	gittest02_text.txt

###2 git add --dry-run . 
[ Working ファイル確認 ( これから Staging に登録されるファイル ) ]

$ git add --dry-run . 
add 'gittest02_text.txt'

###3 git ls-files 
[ Staging ファイル確認 ( まだ gittest01_text.txt のみ ) ]

$ git ls-files
gittest01_text.txt

###4 git status 
[ Working と Staging ファイルの Commit 状態確認 ( Working に gittest02_text.txtファイルが Untracked ) ]

$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	gittest02_text.txt

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

###5 git add . 
[ Working ファイル全てを Staging へ ファイル登録( add ) 実行 ]

$ git add .

###6 git ls-files 
[ Staging ファイル確認 ( gittest02_text.txt が登録された ) ]

$ git ls-files
gittest01_text.txt
gittest02_text.txt

###7 git status 
[ Working と Staging ファイルの Commit 状態確認 ( Staging に登録され、Local Repository に Commit できる状態 ) ]

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   gittest02_text.txt

###8 git diff --staged 
[ Staging と Local Repository の差分 確認 ( Staging にファイルが登録され、今度は Local Repository との差分がある状態 ) ]

$ git diff --staged
diff --git a/gittest02_text.txt b/gittest02_text.txt
new file mode 100644
index 0000000..e69de29

###9 git commit -m "gittest02_text.txtをコミット" 
[ コメントを付けて Staging のファイルを Local Repository へ ファイル登録( commit ) 実行 ]

$ git commit -m "gittest02_text.txtをコミット"
[master c180d67] gittest02_text.txtをコミット
 Committer: user <user@user-no-MacBook-Pro.local>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 gittest02_text.txt

###10 git status  
[ Working と Staging ファイルの Commit 状態確認 ( Local Repository が最新であるため、何も問題が無い状態 ) ]

$ git status
On branch master
nothing to commit, working tree clean

###11 git diff --staged  
[ Staging と Local Repository の差分確認 ( 差分が無くなった ) ]

$ git diff --staged

###12 git show  
[ 最新 Commit 内容表示 ]

$ git show
commit c180d675574d694f985d81d9fb8462c5215127df (HEAD -> master)
Author: user <user@user-no-MacBook-Pro.local>
Date:   Tue Nov 13 22:48:31 2018 +0800

    gittest02_text.txtをコミット

diff --git a/gittest02_text.txt b/gittest02_text.txt
new file mode 100644
index 0000000..e69de29

###13 git reflog  
[ Branch切り替え確認 ]

$ git reflog
c180d67 (HEAD -> master) HEAD@{0}: commit: gittest02_text.txtをコミット
0938823 (origin/master) HEAD@{1}: commit (initial): 初めてのコミット

###14 git ls-remote

$ git ls-remote
From https://github.com/user/gittest01.git
0938823094d05ddb762b39d8dae1563d8a2c2376	HEAD
0938823094d05ddb762b39d8dae1563d8a2c2376	refs/heads/master

###15 git push origin master 
[ Remote Repository に Local Repository を反映 ]

$ git push origin master
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 320 bytes | 320.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/user/gittest01.git
   0938823..c180d67  master -> master

###16 GitHub にて登録を確認

練習 2: 2回目以降 : Fin



練習 3 : プッシュ済みファイル再編集/再プッシュ

###1 git status

$ 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")

###2 git add --dry-run .

$ git add --dry-run .
add 'index.html'

###3 git ls-files

$ git ls-files
.DS_Store
img/.DS_Store
img/Top.png
img/about bg.png
img/mypic.png
img/sunset_sky.jpg
img/work 1 bg.png
img/work 1 mockup.png
img/work 2 bg.png
img/work 2 mockup.png
"img/work 3 bg yuen \347\265\220\347\270\201.png"
img/work 3 mockup.png
index.html
js/0077-jquery.scrollify.js
js/0077-menu_js.js
js/own.js
movie/.DS_Store
movie/bg_movie.mp4
style.css

###4 git add . / git status

$ git add .

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   index.html

###5 git diff --staged

$ git diff --staged
diff --git a/index.html b/index.html
index fcea6a3..8a1a0f7 100644
--- a/index.html
+++ b/index.html
@@ -5,7 +5,7 @@
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <meta http-equiv="X-UA-Compatible" content="ie=edge">
   
-  <title>0077 - Portfolio v2</title>
+  <title>User Portfolio</title>
   
   <link rel="stylesheet" href="style.css">
   <link href="https://fonts.googleapis.com/css?family=Herr+Von+Muellerhoff|Pinyon+Script" rel="stylesheet">

###6 git commit "changed title" / git diff --staged

$ git commit -m "changed title"
[master 7ce68c6] changed title
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git diff --staged

###7 git log --stat

$ git log --stat
commit 7ce68c63eb053db5d810a1dd7201d39afa2d698f (HEAD -> master)
Author: User <*****+user@users.noreply.github.com>
Date:   Sat Nov 24 12:15:22 2018 +0800

    changed title

 index.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

commit 9751b6b3c684db39254fce57f62688c7a23f03a6 (origin/master)
Author: user <*****+user@users.noreply.github.com>
Date:   Sat Nov 24 11:13:39 2018 +0800

    portfolio_template_v1

 .DS_Store                                         | Bin 0 -> 6148 bytes
 img/.DS_Store                                     | Bin 0 -> 6148 bytes
 img/Top.png                                       | Bin 0 -> 604954 bytes
 img/about bg.png                                  | Bin 0 -> 380345 bytes
 img/mypic.png                                     | Bin 0 -> 336886 bytes
 img/sunset_sky.jpg                                | Bin 0 -> 149108 bytes
 img/work 1 bg.png                                 | Bin 0 -> 229532 bytes
 img/work 1 mockup.png                             | Bin 0 -> 102057 bytes
 img/work 2 bg.png                                 | Bin 0 -> 514334 bytes
 img/work 2 mockup.png                             | Bin 0 -> 211844 bytes
 "img/work 3 bg yuen \347\265\220\347\270\201.png" | Bin 0 -> 317451 bytes
 img/work 3 mockup.png                             | Bin 0 -> 154553 bytes
 index.html                                        | 161 +++++++++
 js/0077-jquery.scrollify.js                       | 847 ++++++++++++++++++++++++++++++++++++++++++++++++
 js/0077-menu_js.js                                |  51 +++
 js/own.js                                         |  66 ++++
 movie/.DS_Store                                   | Bin 0 -> 6148 bytes
 movie/bg_movie.mp4                                | Bin 0 -> 2029016 bytes
 style.css                                         | 187 +++++++++++
 19 files changed, 1312 insertions(+)

###8 git push origin master

$ git push origin master
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 335 bytes | 335.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/user/portfolio.git
   9751b6b..7ce68c6  master -> master 



練習 4 : add と commit を一気に実行

###1 ファイル編集 / git status

$ 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") 

###2 git commit -a -m "changed title at 2nd"

$ git commit -a -m "changed title at 2nd"
[master 6069f92] changed title at 2nd
 1 file changed, 1 insertion(+), 1 deletion(-) 

###3 git status / git diff --staged / git show

$ git status
On branch master
nothing to commit, working tree clean 

$ git diff --staged


$ git show
commit 6069f92c33947e60361b03a208e2e3e779d72cb2 (HEAD -> master)
Author: User <*****+user@users.noreply.github.com>
Date:   Sat Nov 24 12:32:57 2018 +0800

    changed title at 2nd

diff --git a/index.html b/index.html
index 8a1a0f7..aae45f0 100644
--- a/index.html
+++ b/index.html
@@ -5,7 +5,7 @@
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <meta http-equiv="X-UA-Compatible" content="ie=edge">
   
-  <title>User Portfolio</title>
+  <title>Portfolio / User </title>
   
   <link rel="stylesheet" href="style.css">
   <link href="https://fonts.googleapis.com/css?family=Herr+Von+Muellerhoff|Pinyon+Script" rel="stylesheet"> 

###4 git push origin master

$ git push origin master
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 349 bytes | 349.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/user/portfolio_template_v1.git
   7ce68c6..6069f92  master -> master



33
42
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
33
42