# git 實作練習 5 ## 目錄 - [1. 開發者 : 產生個人 SSH 金鑰](#1-開發者--產生個人-ssh-金鑰) - [1.1. Linux 平台](#11-linux-平台) - [1.2. Windows 平台 (gitbash)](#12-windows-平台-gitbash) - [1.3. maxOS 平台](#13-maxos-平台) - [2. 管理員 : git 主機, 新增共用帳號 `g` (Linux)](#2-管理員--git-主機-新增共用帳號-g-linux) - [2.1. 新增帳號 g](#21-新增帳號-g) - [2.2. 所有開發者的公鑰, 匯入 `g` 的 authorized_keys](#22-所有開發者的公鑰-匯入-g-的-authorized_keys) - [3. 管理員 : 遠端倉庫 (Linux)](#3-管理員--遠端倉庫-linux) - [3.1. repo 主機, 建立遠端倉庫並以 --bare 初始化](#31-repo-主機-建立遠端倉庫並以---bare-初始化) - [3.2. **重要** 不准帳號 `g` 登入 shell](#32-重要-不准帳號-g-登入-shell) - [4. 指定開發者 : 建立原始基礎程式](#4-指定開發者--建立原始基礎程式) - [4.1. 原始基礎程式](#41-原始基礎程式) - [4.2. 建立全新 laravel 專案 (可跳過)](#42-建立全新-laravel-專案-可跳過) - [4.3. git init 及 commit 原始基礎程式](#43-git-init-及-commit-原始基礎程式) - [5. 指定開發者 : 第一版原始基礎程式與 repo 的連結](#5-指定開發者--第一版原始基礎程式與-repo-的連結) - [5.1. 狀況一 : 原始基礎程式與 repo 主機相同, 都在 Linux](#51-狀況一--原始基礎程式與-repo-主機相同-都在-linux) - [5.2. 狀況二 : 原始基礎程式在 Windows , repo 主機在 Linux](#52-狀況二--原始基礎程式在-windows--repo-主機在-linux) - [5.3. 狀況三 : 原始基礎程式在 macOS , repo 主機在 Linux](#53-狀況三--原始基礎程式在-macos--repo-主機在-linux) - [5.4. 遠端倉庫第一次 push 之後, 跟先前原始 bare 時統計比對](#54-遠端倉庫第一次-push-之後--跟先前原始-bare-時統計比對) - [6. 本章總結](#6-本章總結) - [6.1. 本章使用 git 命令總結](#61-本章使用-git-命令總結) - [6.2. 好用的連結](#62-好用的連結) @ver 0.1.1 @date 2019-04-19 21:37:21 (星期五) @ver 0.2.1 @date 2022-05-21 18:26:43 (星期六) @ver 0.2.2 @date 2022-06-13 13:33:55 (星期1) > 上一篇實作遠端倉庫, 衝突合併後, 不同的開發者出現寫入權限問題 > > 這一篇我們重頭再來, 但透過 ssh 使用一個共用的帳號, 就叫它 `g` , **注意** 帳號就一個字 `g` , git 的第一個英文字, 可以少打幾個字 參考來源 : [git-scm 4.2 伺服器上的 Git - 在伺服器上佈署 Git](https://git-scm.com/book/zh-tw/v2/伺服器上的-Git-在伺服器上佈署-Git) [git-scm 4.3 伺服器上的 Git - 產生你的 SSH 公鑰](https://git-scm.com/book/zh-tw/v2/伺服器上的-Git-產生你的-SSH-公鑰) [git-scm 4.4 伺服器上的 Git - 設定伺服器](https://git-scm.com/book/zh-tw/v2/伺服器上的-Git-設定伺服器) ## 1. 開發者 : 產生個人 SSH 金鑰 {#1-開發者--產生個人-ssh-金鑰} > 每位開發者請使用 ssh-keygen 各自在自己的開發機器上建立 以下分別在三種不同作業系統平台執行 ### 1.1. Linux 平台 {#11-linux-平台} #### 1.1.1. 命令 ```shell cd ~ mkdir .ssh chmod 700 .ssh cd .ssh ls -la ssh-keygen -t rsa # 一直按 ENTER 直到結束 ls -la # 可以看到 `公開金鑰 id_rsa.pub` 及 `私密金鑰 id_rsa` 兩個檔案 # 以下文章簡稱 `公鑰` 及 `私鑰` ``` #### 1.1.2. 執行結果 ```shell [jazz@aaroni ~ ]$ cd ~ [jazz@aaroni ~ ]$ mkdir .ssh [jazz@aaroni ~ ]$ chmod 700 .ssh [jazz@aaroni ~ ]$ cd .ssh [jazz@aaroni ~/.ssh ]$ ls -la 總計 8 drwx------ 2 jazz admin 4096 4月 13 08:04 . drwxr-xr-x 9 jazz admin 4096 4月 19 13:29 .. [jazz@aaroni ~/.ssh ]$ [jazz@aaroni ~/.ssh ]$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/jazz/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/jazz/.ssh/id_rsa. Your public key has been saved in /home/jazz/.ssh/id_rsa.pub. The key fingerprint is: SHA256:8QjjdgpuJBgbfYEPZpeSofmM16BDy59eyVdop643LTg jazz@aaroni.org.tw The key's randomart image is: +---[RSA 2048]----+ | .+.. | | +B o. | |=+o=. o . | |oX.+.. o.+ | |*o= + ooSo. | | o.+oooo+ | | oo+oo. | | ...Eo+ . | | . .+.o | +----[SHA256]-----+ [jazz@aaroni ~/.ssh ]$ [jazz@aaroni ~/.ssh ]$ ls -la 總計 16 drwx------ 2 jazz admin 4096 4月 19 22:10 . drwxr-xr-x 9 jazz admin 4096 4月 19 13:29 .. -rw------- 1 jazz admin 1675 4月 19 22:10 id_rsa -rw-r--r-- 1 jazz admin 399 4月 19 22:10 id_rsa.pub [jazz@aaroni ~/.ssh ]$ ``` 複製一份自己的 `公鑰 id_rsa.pub` 到 `/tmp/` 目錄下, 但每個人要先改名, 避免檔名重複. 稍後給共用帳號 `g` 加入到它的 `authorized_keys` 讓 git 認證用, 完成後即可刪除 例如 : `cp ~/.ssh/id_rsa.pub /tmp/id_rsa.jazz.pub` ### 1.2. Windows 平台 (gitbash) {#12-windows-平台-gitbash} 請先進入 gitbash, 在此應用程式下操作 #### 1.2.1. 命令 ```shell # 請先進入 gitbash ssh-keygen # 一直按 ENTER 直到結束 ls -l ~/.ssh/ # 可以看到 `公開金鑰 id_rsa.pub` 及 `私密金鑰 id_rsa` 兩個檔案 # 以下文章簡稱 `公鑰` 及 `私鑰` # 把 `公鑰` 加入 git 主機 ssh-copy-id jazz@173.255.208.139 # 此時 `公鑰` 尚未傳入主機, 仍需要輸入密碼 # 完成後, 試試看 ssh 直接登入 ssh jazz@173.255.208.139 # 此時已不用輸入密碼 # 登入主機後, 複製一份自己的 `公鑰 id_rsa.pub` 到 `/tmp/` 目錄下, 但每個人要先改名, 避免檔名重複. 稍後給共用帳號 `g` 加入到它的 `authorized_keys` 讓 git 認證用, 完成後即可刪除 `/tmp/jazz.authorized_keys` # 因為目前是透過 ssh-copy-id 上傳到主機, 所以 .ssh 目錄下, 只會有一個 authorized_keys 檔案, 但其實它就是公鑰 id_rsa.pub 的組合檔 cp ~/.ssh/authorized_keys /tmp/jazz.authorized_keys chmod 644 /tmp/jazz.authorized_keys # 複製一份到 /tmp/ 之下, 同時 chmod 改為 644, 讓 `g` 可讀 exit # 返回到 Windows gitbash ``` #### 1.2.2. 執行結果 ```shell incloud@eDocService MINGW64 ~ $ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/incloud/.ssh/id_rsa): Created directory '/c/Users/incloud/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /c/Users/incloud/.ssh/id_rsa Your public key has been saved in /c/Users/incloud/.ssh/id_rsa.pub The key fingerprint is: SHA256:vOw4oVEloxtM4aVZ6S2l8iAG1IsaIDZQrqfaQwKQAUs incloud@eDocService The key's randomart image is: +---[RSA 3072]----+ |BEo ..o. | |B= o.=+ o | |*.+o=o B | |ooo.* =.. | |+o.. B .S | |oo. o o. . | |.o o .o | |... . .o | |. .. ... | +----[SHA256]-----+ incloud@eDocService MINGW64 ~ $ ls -l ~/.ssh/ total 5 -rw-r--r-- 1 incloud 197121 2610 May 31 14:57 id_rsa -rw-r--r-- 1 incloud 197121 573 May 31 14:57 id_rsa.pub incloud@eDocService MINGW64 ~ $ ssh-copy-id jazz@173.255.208.139 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/c/Users/incloud/.ssh/id_rsa.pub" The authenticity of host '173.255.208.139 (173.255.208.139)' can't be established. ED25519 key fingerprint is SHA256:aWHgOrrrO7rO1x1NBLq0q6K5rHkofTzA78Z3xhqSsYE. This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys jazz@173.255.208.139's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'jazz@173.255.208.139'" and check to make sure that only the key(s) you wanted were added. incloud@eDocService MINGW64 ~ $ $ ssh jazz@173.255.208.139 Welcome to Ubuntu 18.04.6 LTS (GNU/Linux 5.17.5-x86_64-linode154 x86_64) ... ... ... jazz@aaroni:~$ jazz@aaroni:~$ cp ~/.ssh/authorized_keys /tmp/jazz.authorized_keys jazz@aaroni:~$ jazz@aaroni:~$ chmod 644 /tmp/jazz.authorized_keys jazz@aaroni:~$ exit ``` ### 1.3. maxOS 平台 {#13-maxos-平台} #### 1.3.1. 命令 ```shell cd ~ mkdir .ssh chmod 700 .ssh cd .ssh ls -la ssh-keygen -t rsa # 一直按 ENTER 直到結束 ls -la # 可以看到 `公開金鑰 id_rsa.pub` 及 `私密金鑰 id_rsa` 兩個檔案 # 以下文章簡稱 `公鑰` 及 `私鑰` ``` #### 1.3.2. 執行結果 ```shell [robot@mac : ~ ]$ cd ~ [robot@mac : ~ ]$ mkdir .ssh [robot@mac : ~ ]$ chmod 700 .ssh [robot@mac : ~ ]$ cd .ssh [robot@mac : ~/.ssh ]$ ls -la 總計 8 drwx------ 2 robot staff 4096 6月 1 08:04 . drwxr-xr-x 9 robot staff 4096 6月 1 13:29 .. [robot@mac : ~/.ssh ]$ [robot@mac : ~/.ssh ]$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/Users/robot/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /Users/robot/.ssh/id_rsa. Your public key has been saved in /Users/robot/.ssh/id_rsa.pub. The key fingerprint is: SHA256:8QjjdgpuJBgbfYEPZpeSofmM16BDy59eyVdop643LTg robot@mac : .org.tw The key's randomart image is: +---[RSA 2048]----+ | .+.. | | +B o. | |=+o=. o . | |oX.+.. o.+ | |*o= + ooSo. | | o.+oooo+ | | oo+oo. | | ...Eo+ . | | . .+.o | +----[SHA256]-----+ [robot@mac : ~/.ssh ]$ [robot@mac : ~/.ssh ] 🔱 ➠ $ ls -la total 96 drwx------ 11 robot staff 352 5 27 21:17 . drwxr-xr-x+ 137 robot staff 4384 6 1 10:51 .. -rw------- 1 robot staff 1766 6 1 22:23 id_rsa -rw-r--r-- 1 robot staff 396 6 1 22:23 id_rsa.pub [robot@mac : ~/.ssh ] 🔱 ➠ $ ``` ## 2. 管理員 : git 主機, 新增共用帳號 `g` (Linux) {#2-管理員--git-主機-新增共用帳號-g-linux} ### 2.1. 新增帳號 g {#21-新增帳號-g} > 注意 : 此帳號 `g` 不用 ssh-keygen 建立 `公鑰` 及 `私鑰` #### 2.1.1. 命令 ```shell sudo -i adduser g su g cd mkdir .ssh ls -la .ssh ``` #### 2.1.2. 執行結果 ```shell [alex@aaroni ~ ]$ sudo -i [sudo] password for alex: [root@aaroni ~ ]# adduser g ...(略) Is the information correct? [Y/n] [root@aaroni ~ ]# root@aaroni:~# su g g@aaroni:/root$ cd g@aaroni:~$ mkdir .ssh g@aaroni:~$ g@aaroni:~$ ls -la .ssh 總計 8 drwxr-xr-x 2 g g 4096 4月 19 22:02 . drwxr-xr-x 3 g g 4096 4月 19 22:02 .. g@aaroni:~$ ``` ### 2.2. 所有開發者的公鑰, 匯入 `g` 的 authorized_keys {#22-所有開發者的公鑰-匯入-g-的-authorized_keys} **重要** 只要執行一次. 將放在 `/tmp/` 的開發者公鑰加入 `g` 的 `~/.ssh/authorized_keys` #### 2.2.1. 命令 ```shell ls -l /tmp/*.pub cat /tmp/id_rsa.alex.pub >> ~/.ssh/authorized_keys cat /tmp/id_rsa.kevin.pub >> ~/.ssh/authorized_keys cat /tmp/id_rsa.logo.pub >> ~/.ssh/authorized_keys cat /tmp/id_rsa.maxe.pub >> ~/.ssh/authorized_keys cat /tmp/jazz.authorized_keys >> ~/.ssh/authorized_keys ...其他開發請自行想辦法操作加入 :-D ls -l .ssh/authorized_keys g@aaroni:~$ cat .ssh/authorized_keys ``` #### 2.2.2. 執行結果 ```shell g@aaroni:~$ ll /tmp/*.pub -rw-r--r-- 1 alex alex 393 5月 24 09:48 /tmp/id_rsa.alex.pub -rw-r--r-- 1 kevin kevin 394 5月 24 09:58 /tmp/id_rsa.kevin.pub -rw-r--r-- 1 logo logo 393 5月 24 09:48 /tmp/id_rsa.logo.pub -rw-r--r-- 1 maxe maxe 393 5月 24 09:55 /tmp/id_rsa.maxe.pub g@aaroni:~$ g@aaroni:~$ cat /tmp/id_rsa.alex.pub >> ~/.ssh/authorized_keys g@aaroni:~$ cat /tmp/id_rsa.kevin.pub >> ~/.ssh/authorized_keys g@aaroni:~$ cat /tmp/id_rsa.logo.pub >> ~/.ssh/authorized_keys g@aaroni:~$ cat /tmp/id_rsa.maxe.pub >> ~/.ssh/authorized_keys g@aaroni:~$ cat /tmp/jazz.authorized_keys >> ~/.ssh/authorized_keys g@aaroni:~$ g@aaroni:~$ ls -l .ssh/authorized_keys -rw-rw-r-- 1 g g 1573 5月 24 10:02 .ssh/authorized_keys g@aaroni:~$ g@aaroni:~$ cat .ssh/authorized_keys ...(略) ``` ## 3. 管理員 : 遠端倉庫 (Linux) {#3-管理員--遠端倉庫-linux} ### 3.1. repo 主機, 建立遠端倉庫並以 --bare 初始化 {#31-repo-主機-建立遠端倉庫並以---bare-初始化} **重要** 只要執行一次. 假設我們把遠端倉庫放在 `/web/repo`, 以專用的 `g` 使用者身份建立 #### 3.1.1. 命令 ```shell # 視狀況而定, 更換為 root 身份 sudo -i cd / mkdir web chown 1002:1002 web/ ls -ld web/ # 切換為帳號 g su g cd /web mkdir repo cd repo git init --bare ls -la cd .. tree -f repo/ du -csb repo/ ``` #### 3.1.2. 執行結果及說明 ```shell # 更換為 root 身份 [root@aaroni / ]# mkdir web [root@aaroni / ]# chgrp 1002:1002 web/ [root@aaroni / ]# ls -ld web/ drwxr-xr-x 2 g g 4096 5月 24 10:14 web/ [root@aaroni / ]# # 切換為帳號 g [root@aaroni / ]# su g g@aaroni:/$ cd /web/ # 建立遠端倉庫子目錄, 名為 `repo` g@aaroni:/web$ mkdir repo # 進入子目錄 g@aaroni:/web$ cd repo # 遠端倉庫初始化 --bare g@aaroni:/web/repo$ git init --bare Initialized empty Git repository in /web/repo/ g@aaroni:/web/repo$ # 查看 g@aaroni:/web/repo$ ls -la total 40 drwxrwxr-x 7 g g 4096 5月 24 10:20 . drwxr-xr-x 3 g g 4096 5月 24 10:19 .. drwxrwxr-x 2 g g 4096 5月 24 10:20 branches -rw-rw-r-- 1 g g 66 5月 24 10:20 config -rw-rw-r-- 1 g g 73 5月 24 10:20 description -rw-rw-r-- 1 g g 23 5月 24 10:20 HEAD drwxrwxr-x 2 g g 4096 5月 24 10:20 hooks drwxrwxr-x 2 g g 4096 5月 24 10:20 info drwxrwxr-x 4 g g 4096 5月 24 10:20 objects drwxrwxr-x 4 g g 4096 5月 24 10:20 refs g@aaroni:/web/repo$ # 統計 repo 有 9 個目錄, 15 個檔案 g@aaroni:/web/repo$ cd .. g@aaroni:/web$ tree -f repo/ ...(略) 9 directories, 15 files g@aaroni:/web$ # 統計總 Bytes 為 60210 g@aaroni:/web$ du -cshb repo 60210 repo 60210 total g@aaroni:/web$ ``` > _共同開發者們, 權限讀寫問題_ > > **重要** : 因為以後所有 `git push 或 pull` 的帳號都會是 `g` 所以不用處理個別權限可讀寫的問題. 只要 `user g` 可讀寫即可. ### 3.2. **重要** 不准帳號 `g` 登入 shell {#32-重要-不准帳號-g-登入-shell} > 注意 : 請在完成 `g` 的遠端倉庫並初始化後再做此設定. git 有一個額外的防範措施, 它內建 git-shell 工具限制此帳號的活動範圍. 只要把 login shell 設為 `/usr/bin/git-shell` 用戶就無法正常登入系統, 以避免不必要的問題. #### 3.2.1. 命令 ```shell sudo vipw ``` #### 3.2.2. 執行結果 ```shell g:x:1002:1002:,,,:/home/g:/bin/bash # 改為 g:x:1002:1002:,,,:/home/g:/usr/bin/git-shell ``` ## 4. 指定開發者 : 建立原始基礎程式 {#4-指定開發者--建立原始基礎程式} ### 4.1. 原始基礎程式 {#41-原始基礎程式} **重要** 指定一位開發者, 只要執行一次 > 以 laravel 專案為例, 若為其他不同型態或是舊的專案, 請建立好直接跳到下一小節 ### 4.2. 建立全新 laravel 專案 (可跳過) {#42-建立全新-laravel-專案-可跳過} 假設現在是 `alex`. 從頭開始建立一個新的 laravel 專案, 暫時先叫做 `firstcrm`, 名稱不重要, 這只會當作基礎起點框架上傳到遠端倉庫. 後續此所有共同開發者, 都將會在每個人各自家目錄 clone 回來的專案目錄裡修改操作, 測試完成後, 再 push 回遠端的 git repo. 建立遠端倉庫用的 laravel 基礎框架時, 先不處理資料庫設定, 但之後各自要把它完成. 每位開發者可以有各自開發用資料庫, 公用測試版也有它的資料庫. 當然, 網站正式上線一定會有正式版專用的資料庫. 同時也不處理 `.env` , `node_modules` 及 `vendor` ...等, 這些檔案都被禁止或者不需要上傳至遠端倉庫, 前者是安全問題, 後者是跟隨原開發者會有經常性變動. 詳細可查閱 `.gitignore` 檔案. #### 4.2.1. 命令及說明 ```shell [alex@aaroni ~ ]$ laravel new firstcrm ...(略) Application ready! Build something amazing. # 等待約 60 秒 ... 完成 [alex@aaroni ~ ]$ cd firstcrm/ [alex@aaroni ~/firstcrm ]$ php artisan -V # 版本 Laravel Framework 9.14.0 # 修改系統基礎設定 [alex@aaroni ~/firstcrm ]$ vi config/app.php # 時間 timezone 預設 UTC, 改為 Asia/Taipei # 本地化 locale 預設 en, 改為 zh-TW # (可不用修改) 另外有一個 'fallback_locale' => 'en', 這個是指如果 'zh-TW' 找不到的話, 它要找誰的語言來替代顯示, 因為所有各種版本都一定有英文對應文字, 所以就用 'en' 即可, 改成 'zh-TW' 或其他語言也沒有什麼問題, 只是可能會顯示空白, 也許剛好可以拿來測試未翻譯本地化的文字. [alex@aaroni ~/firstcrm ]$ vi config/database.php [alex@aaroni ~/firstcrm ]$ vi config/logging.php ...(略) ``` ### 4.3. git init 及 commit 原始基礎程式 {#43-git-init-及-commit-原始基礎程式} ```shell # 複製完整符合需求的 .gitignore cp ~/job/0rz/.gitignore . # git 初始化 git init # Initialized empty Git repository in /home/alex/firstcrm/.git/ git add . # 本地第一次提交 git commit -m '創立遠端倉庫用的 laravel 基礎框架 9.14.0' git log [alex@aaroni ~/firstcrm ]$ git log 1 commit 2b3aff27692d1d1a3e08e950fd9e20d4fc872221 (HEAD -> master) 2 Author: alex 3 Date: Fri May 27 13:23:50 2022 +0800 4 5 創立遠端倉庫用的 laravel 基礎框架 9.14.0 [alex@aaroni ~/firstcrm ]$ ``` ## 5. 指定開發者 : 第一版原始基礎程式與 repo 的連結 {#5-指定開發者--第一版原始基礎程式與-repo-的連結} > **重要** : 設定 `本地倉庫` 到 `遠端倉庫` 的連結, 並 push 到 `遠端倉庫` ### 5.1. 狀況一 : 原始基礎程式與 repo 主機相同, 都在 Linux {#51-狀況一--原始基礎程式與-repo-主機相同-都在-linux} 以剛剛建立的 firstcrm 為例 #### 5.1.1. 命令 ```shell # git 加入遠端倉庫連結 (IP 可能會變動) # 因為現在都同一台主機, 所以 IP 用 127.0.0.1 git remote add origin g@127.0.0.1:/web/repo # git push 遠端倉庫 git push origin master ``` #### 5.1.2. 執行結果 ```shell # 進入專案目錄 [alex@aaroni ~ ]$ cd firstcrm/ # 查看現有的 git config 設定檔 [alex@aaroni ~/firstcrm ]$ cat .git/config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [alex@aaroni ~/firstcrm ]$ # 加入遠端倉庫連結, 因為是本機, 所以 IP 使用 127.0.0.1 [alex@aaroni ~/firstcrm ]$ git remote add origin g@127.0.0.1:/web/repo # 查看加入後的 git config 設定檔 [alex@aaroni ~/firstcrm ]$ cat .git/config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [remote "origin"] url = g@127.0.0.1:/web/repo fetch = +refs/heads/*:refs/remotes/origin/* [alex@aaroni ~/firstcrm ]$ # 第一次 `push` 會要求 `The authenticity of host '127.0.0.1 (127.0.0.1)'...` 的授權, 請輸入 `yes` 即可. [alex@aaroni ~/firstcrm ]$ git push origin master The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established. ECDSA key fingerprint is SHA256:pDHo1ly0HbXNov7lsRcU0ILzDvCju+R1CiZaCQaUFgg. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '127.0.0.1' (ECDSA) to the list of known hosts. Counting objects: 107, done. Compressing objects: 100% (88/88), done. remote: Resolving deltas: 100% (7/7), done. Writing objects: 100% (107/107), 72.07 KiB | 1.64 MiB/s, done. Total 107 (delta 7), reused 0 (delta 0) To 127.0.0.1:/web/repo * [new branch] master -> master [alex@aaroni ~/firstcrm ]$ ``` ### 5.2. 狀況二 : 原始基礎程式在 Windows , repo 主機在 Linux {#52-狀況二--原始基礎程式在-windows--repo-主機在-linux} 另外建一個實驗程式及實驗的 repo #### 5.2.1. 命令 ```shell # git 加入遠端倉庫連結 (IP 可能會變動) # 因為現在都同一台主機, IP 使用 173.255.208.139 git remote add origin g@173.255.208.139:/web/repo # git push 遠端倉庫 git push origin master ``` #### 5.2.2. 執行結果 ```shell # gitbash 進入專案程式目錄 da2233ad@WA MINGW64 ~ $ cd /c/temp/kit # 現在程式 da2233ad@WA MINGW64 /c/temp/kit (master) $ ls -l total 2 -rw-r--r-- 1 da2233ad 197121 51 Jun 9 18:29 README.md -rw-r--r-- 1 da2233ad 197121 42 Jun 9 18:32 i.php drwxr-xr-x 1 da2233ad 197121 0 Jun 9 18:30 output/ # git log da2233ad@WA MINGW64 /c/temp/kit (master) $ git log commit d054e25abcd9f876d577db7bdf5785ccc819f61f (HEAD -> master) Author: da2233ad Date: Thu Jun 9 18:32:54 2022 +0800 變動 commit a787c24bd20cf80b515bf089b122a2bf78dd31dd Author: da2233ad Date: Thu Jun 9 18:31:57 2022 +0800 初始 # 查看現有的 git config 設定檔 $ cat .git/config [core] repositoryformatversion = 0 filemode = false bare = false logallrefupdates = true ignorecase = true # 加入遠端倉庫連結, IP 使用 173.255.208.139 da2233ad@WA MINGW64 /c/temp/kit (master) $ git remote add origin g@173.255.208.139:~/repo/kit # 再度查看 git config 設定檔 da2233ad@WA MINGW64 /c/temp/kit (master) $ cat .git/config [core] repositoryformatversion = 0 filemode = false bare = false logallrefupdates = true ignorecase = true [remote "origin"] url = g@173.255.208.139:~/repo/kit fetch = +refs/heads/*:refs/remotes/origin/* # git push 遠端倉庫 # 發生錯誤, 因為 ~/repo/kit 子目錄尚未建立 da2233ad@WA MINGW64 /c/temp/kit (master) $ git push origin master fatal: '~/repo/kit' does not appear to be a git repository fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. da2233ad@WA MINGW64 /c/temp/kit (master) $ # ... # 到 linux repo 主機, 以 g 的身份, 建立 ~/repo/kit, 並 git init --bare # ...(略) # 完成後, 再度 git push 遠端倉庫 da2233ad@WA MINGW64 /c/temp/kit (master) $ git push origin master Enumerating objects: 10, done. Counting objects: 100% (10/10), done. Delta compression using up to 2 threads Compressing objects: 100% (5/5), done. Writing objects: 100% (10/10), 1.44 KiB | 736.00 KiB/s, done. Total 10 (delta 1), reused 0 (delta 0), pack-reused 0 To 173.255.208.139:~/repo/kit * [new branch] master -> master da2233ad@WA MINGW64 /c/temp/kit (master) $ ``` ### 5.3. 狀況三 : 原始基礎程式在 macOS , repo 主機在 Linux {#53-狀況三--原始基礎程式在-macos--repo-主機在-linux} 另外建一個實驗程式及實驗的 repo #### 5.3.1. 命令 ```shell # git 加入遠端倉庫連結 (IP 可能會變動) # 因為現在都同一台主機, IP 使用 173.255.208.139 git remote add origin g@173.255.208.139:/web/repo # git push 遠端倉庫 git push origin master ``` #### 5.3.2. 執行結果 ```shell ...(略) ``` ### 5.4. 遠端倉庫第一次 push 之後, 跟先前原始 bare 時統計比對 {#54-遠端倉庫第一次-push-之後--跟先前原始-bare-時統計比對} ```shell # 統計 repo 有 9 個目錄, 17 個檔案, 原先只有 15 個檔案 [alex@aaroni /web ]$ tree -f repo/ ...(略) 9 directories, 18 files # 統計總 Bytes 為 138119, 原先只有 60210 Bytes [alex@aaroni /web ]$ du -cshb repo 138119 repo 138119 total [alex@aaroni /web ]$ ``` ## 6. 本章總結 {#6-本章總結} ### 6.1. 本章使用 git 命令總結 {#61-本章使用-git-命令總結} ```shell ssh-keygen -t rsa # 產生個人公鑰及私鑰 ssh-copy-id user@ooo.xxx.yyy.zzz # 把 `公鑰` 加入 git 主機 git init --bare # 遠端 bare 倉庫初始化 git remote add origin g@127.0.0.1:/web/repo # 設定 git 本地倉庫到遠端倉庫的連結, ssh 共用帳號 g git remote add origin 127.0.0.1:/home/repo # 設定 git 本地倉庫到遠端倉庫的連結, 原始版 git clone g@127.0.0.1:/web/repo myPrj # 複製遠端倉庫回家, 指定目錄名稱, ssh 共用帳號 g git clone /home/repo myPrj # 複製遠端倉庫回家, 指定目錄名稱, 原始版 git push origin master # 本地倉庫(分支 master)提交後, 推入遠端倉庫(origin 的分支 master) git pull origin master # 拉回遠端倉庫(origin 的分支 master)最後版本到本地倉庫(分支 master) git fetch origin master # 取得遠端倉庫分支 master 的程式碼 git merge origin/master # 合併本地倉庫分支 origin/master 的程式碼 ``` ## 6.2. 好用的連結 {#62-好用的連結} * [Git 面試題](https://gitbook.tw/interview) * [四種將分支與主線同步的方法](https://cythilya.github.io/2018/06/19/git-merge-branch-into-master/) * 以上這兩篇文章寫得不錯, 跟我們此篇內容有比較多的關聯, 大家找時間把它精讀. * [Git的奇技淫巧 - 簡體中文](https://github.com/521xueweihan/git-tips) 此為老外整理 [Most commonly used git tips and tricks.](https://github.com/git-tips/tips) 的簡體中文版, 很實用, 極具參考價值.