--- title: 使用 WSL(安裝 linux) 在 win10 執行 git 和 repo 工具進行專案管理 disqus: hackmd --- 使用 WSL(安裝 linux) 在 win10 執行 git 和 repo 工具進行專案管理 === [TOC] ## 1. 在 windows10 安裝 WSL --- #### 步驟 1 - 啟用 Windows 子系統 Linux 版 在您以系統管理員模式開啟的命令列中,輸入此命令: ![](https://i.imgur.com/xXFp8aW.png) ```gherkin $ wsl.exe --install 重新啟動您的電腦,並進入BIOS ``` 開啟如下BIOS的設定 ![](https://i.imgur.com/qDSq5Hd.jpg) #### 步驟 2 以系統管理員身分開啟 PowerShell 並執行: ~~Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux~~ 替換成 ```gherkin dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart ``` ![](https://i.imgur.com/8tE1YUz.png) #### 步驟 3 - 啟用虛擬機器功能 以系統管理員身分開啟 PowerShell 並執行: ```gherkin dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart ``` ![](https://i.imgur.com/9gH1MWF.png) #### 步驟 4 - 下載 Linux 核心更新套件 下載最新套件: [WSL2 Linux 核心更新套件 (適用於 x64 電腦)](https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi) 安裝 wsl_update_x64.msi #### 步驟 5 - 安裝您選擇的 Linux 發行版本 開啟Microsoft Store ![](https://i.imgur.com/3x7fy5K.png) 搜尋 ubuntu 18.04 ![](https://i.imgur.com/dGLOFk5.png) 安裝 ubuntu 18.04 ![](https://i.imgur.com/fsGX0QW.png) 自行輸入 ![](https://i.imgur.com/iLqzpNH.png) ## 2. 在 windows10 安裝 windows terminal ![](https://i.imgur.com/aP2QZ9T.png) 可由此工具開啟 WSL ![](https://i.imgur.com/L6374Rr.png) ## 3. 確認 WSL 版本 開啟 cmd 輸入指令如下 ```gherkin $ wsl -l -v ``` ![](https://i.imgur.com/Lse21Mr.png) ## 4. 使用 windows terminal 開啟 ubuntu 並安相關套件 可利用terminal 開啟多個頁面 ![](https://i.imgur.com/IeVzLBz.png) ![](https://i.imgur.com/eCIE4ru.png) 相關套件安裝 ```gherkin $ sudo apt-get update $ sudo apt-get install git tig vim tmux ``` #### tmux 測試,下面為指令 ```gherkin= $ tmux 水平分割 $ ctrl + b 放開後接著按下 shift + " 切換到切割頁面 $ ctrl + b 放開後按下 上 or 下 選擇分頁割面 垂直分割 $ ctrl + b 放開後接著按下 shift + % 切換到切割頁面 $ ctrl + b 放開後按下 左 or 右 選擇分頁割面 調整視窗大小 $ ctrl + b 不要放開,此時按下上下左右 離開 $ exit ``` 水平分割 ![](https://i.imgur.com/U18DEjw.png) 垂直分割 ![](https://i.imgur.com/zGEmCEh.png) #### 關閉 提示音效 ```gherkin= vim ~/.vimrc 輸入如下 set noeb vb t_vb= :wq存檔離開 source ~/.vimrc ``` ## 5. 在ubuntu 上產生 SSH key 並交給學長 --- :::info 當上述都完成後,要先產生ssh key,因為之後的測試,再推commit進遠端需要權限!! 這邊的概念是,遠端的github如何知道 使用者(你) 是否有權限對這個倉庫做修改上傳?? ```gherkin= github的機制是,看看這個github遠端帳號有沒有你這台電腦的key,判斷行為如下: 有: => 代表這個github遠端帳號認識你這台電腦,可以讓你做修改上傳 沒有: => 代表這個github遠端帳號根本不知道你是誰,所以你無權限做修改上傳 ``` ::: ### (1) 安裝OPENSSH CLINET ```gherkin= $ sudo apt-get install ssh ``` ### (2) 輸入以下指令產生key ```gherkin= $ ssh-keygen 一路 enter 到完成 key 的產生 ``` ![](https://i.imgur.com/KG7fO6j.png) ### (3) 將public key 交給學長 ```gherkin= 首先到 key 的位置 然後使用ls查看 $ cd /home/your_name/ $ cd ~/.ssh 這邊注意路徑~符號是代表你的家目錄,所以等同於/home/帳號名稱/, 注意 linux 的符號路徑,與 windows 區別如下 ### linux 路徑符號使用 / ### windows 路徑符號使用 \ $ ls 若要查看有沒有隱藏資料夾使用下列指令 $ ls -a ``` ![](https://i.imgur.com/w6sj1VL.png) ```gherkin= 觀察 id_rsa.pub 裡面的內容 $ cat id_rsa.pub ``` ![](https://i.imgur.com/XZ1Mxy0.png) ```gherkin= 再來將這個檔案複製到d槽,或是其他路徑 $ cp id_rsa.pub /mnt/d/ 將這個檔案壓縮如下,交給學長 ``` ![](https://i.imgur.com/fr6P3Jl.png) ![](https://i.imgur.com/JMNEIzy.png) ```gherkin= 學長收到後會把這個key加到github遠端,如下圖, 代表遠端的github帳號已經可以認得你這台電腦了, 所以你這台電腦就有修改這個倉庫的權限了!! ``` ![](https://i.imgur.com/XclpWab.png) ## 6. git ,tig and vim 測試 --- ### (1) 使用 vim 編輯 git 設定檔案 ```gherkin= $ cd ~ $ vim .gitconfig 編輯如下圖,帳號名稱請輸入自己的 ``` ![](https://i.imgur.com/msTuFTG.png) ### (2) 在家目錄,創一個 NTUT 資料夾並進入 ```gherkin= $ cd ~ $ mkdir NTUT $ cd NTUT ``` ### (3) 下載測試用的 source ```gherkin= ### 完整指令查詢請下 $ man git ### 下載測試git來源 若沒有申請KEY在遠端 GITHUB ,用 HTTP 下載: $ git clone https://github.com/masteree108/NTUT_for_testing.git 若已經有申請 KEY 在遠端 GITHUB ,請選用 SSH 下載: $ git clone git@github.com:masteree108/NTUT_for_testing.git $ cd NTUT_for_tesing $ ls 可看到有 README.md 等檔案 ``` ![](https://i.imgur.com/BMfLZcy.png) ### (4) 使用 tig 看看現在有哪些 commit ```gherkin= ### 完整指令查詢請下 $ man tig ### 常用指令如下 $ tig (請在有 .git 的資料夾輸入 tig ) 進入 commit: enter 離開: q 完整顯示 commit 內容: d 往下: j 往上: k 搜尋: /search_name ``` ![](https://i.imgur.com/1Ee739n.png) ![](https://i.imgur.com/XRMmatR.png) ### (5) 使用 vim 工具隨意編輯 README.md ```gherkin= ### 完整指令查詢請下 $ man vim ### 編輯README.md $ vim README.md ### 常用指令如下 編輯此行: i 從下一行開始編輯: o 回覆上一動: u 取消命令: esc 搜尋: /search_name 存檔後離開: :wq (注意有冒號) 不存檔強制離開: :q! (注意有冒號) 到指定行數: :(冒號+行數) 複製: v 選擇要複製的文字,按下y 貼上:將上一動複製的按下p ``` ### (6) 編輯後查詢,並加修改後的檔案加入暫存區(工作區=>暫存區) ```gherkin= 工作區的檔案 git 能知道是否修改 若剛剛有編輯 README.md 成功後,可用 tig 或如下指令觀察 $ git status ``` ![](https://i.imgur.com/MpSID27.png) ```gherkin= $ git add README.md (嚴禁使用 git add * ) 再觀察一次檔案狀態 $ git status ``` ![](https://i.imgur.com/amk5KAX.png) ```gherkin= 使用tig 觀察 $ tig ``` ![](https://i.imgur.com/YnnPoNn.png) ### (7) 將 add 後的檔案提交(commit)到本地倉庫 :::warning 注意第一次使用 git commit 會先詢問使用者是誰?(若已經修改了.gitconfig 並填入相關資料則系統不會提問) ::: ![](https://i.imgur.com/acW96Zw.png) ```gherkin= 請輸入自己的資料,下圖只是範例 ``` ![](https://i.imgur.com/vkgDmSt.png) ```gherkin= $ git commit 若是有裝 vim 預設編輯器會以vim 套件來編輯,紅色框為新增的提交說明 這裡很重要,可以提醒這個 commit 的功能為何,將來找 bug 或找相似的功能會非常好用 編輯後存檔(:wq)離開 ``` ![](https://i.imgur.com/BUnJu14.png) ```gherkin= 在看一下git狀態,因為已經提交到本地倉庫,所以目前工作區為乾淨的 $ git status ``` ![](https://i.imgur.com/fOWnxOv.png) ```gherkin= $ tig 可看到目前增加的commit 的內容,包括何人修改,修改內容與提交說明 ``` ![](https://i.imgur.com/U07W62A.png) ### (7) 將本地倉庫推至遠端倉庫(再次提醒必須key有申請過後才能使用) ```gherkin= $ git push ``` ## 7. 使用 repo 套件下載 專案程式碼 --- :::info 雖然目前有 vott source 和 python track code 兩包,但之後的 code 可能會越來越多,所以使用 repo 來管理整個專案,才不用每次都使用 git 指令一包一包下載 ::: ### (1) 創建資料夾後,使用 repo 下載專案 ```gherkin= 首先安裝 repo 套件 $ cd ~ $ curl https://storage.googleapis.com/git-repo-downloads/repo > ./repo $ chmod +x repo $ sudo cp repo /usr/bin/repo 之後到 c: 建立專案資料夾 $ cd /mnt/c $ mkdir VoTT $ cd VoTT ``` ```gherkin= 管理倉庫來源下載 ``` [VoTT管理專案的倉庫](https://github.com/masteree108/VoTT_NTUT_WIN10_repo/blob/master/default.xml) ![](https://i.imgur.com/bEH32Sk.png) ```gherkin= 若沒有申請 KEY 在遠端 GITHUB,用 HTTPS 下載: $ repo init -u https://github.com/masteree108/VoTT_NTUT_WIN10_repo.git -b master 若已經有申請 KEY 在遠端 GITHUB ,請選用 SSH 下載: $ repo init -u git@github.com:masteree108/VoTT_NTUT_WIN10_repo.git -b master ``` :::warning 若是在下載時有出現 Permissions 0770 for '/home/xxxxx/.ssh/id_rsa' are too open. 可用下列指令修改權限,再重新下載即可 $ chmod 400 ~/.ssh/id_rsa ::: ```gherkin= 現在VoTT的資料夾只有.repo還沒有各別的專案資料夾 $ ls -a 可看到只有.repo資料夾 ![](https://i.imgur.com/ds2YqfI.png) ``` ```gherkin= 更新來源(等同於從遠端下載) $ repo sync sync 後會看到兩包 source code 資料夾 指定連結到遠端的主分支 $ repo start master --all ``` ![](https://i.imgur.com/wWhZ1pO.png) ```gherkin= 關於 repo start master --all 驗證 這是代表本倉庫的分支要連結到遠端的哪個分支.由於目前我這邊沒有設定任何分支,所以以 master 主分支為準 可以使用如下指令觀察 $ cd CVTRK/WIN10 $ git branch -a 可觀察到目前連結到的是 master 分支 ``` ![](https://i.imgur.com/tOEGzwU.png) ### (2) 觀察此專案內容 ```gherkin= repo sync 後可看到兩包資料夾,注意若是以後只是要從遠端更新專案, 只需要下下列指令,就會從遠端將資料更新到本地端 $ repo sync 下載後可看到兩包資料夾 $ ls ``` ```gherkin= 若是要查有沒有隱藏資料夾使用下列指令,可以看有一包.repo $ ls -a ``` ![](https://i.imgur.com/HawINIz.png) ```gherkin= 進入.repo我們可以看到各自的來源是從哪個 github 倉庫過來的 $ cd .repo $ cd manifests $ cat default.xml ``` ![](https://i.imgur.com/m6rLxdV.png) ![](https://i.imgur.com/lyafrZT.png) ```gherkin= 本地端倉庫路的確如上圖綠線設定 ``` ![](https://i.imgur.com/kY8bJbd.png) ## 9. 參考網站 [Windows 10 上適用於 Linux 的 Windows 子系統安裝指南](https://docs.microsoft.com/zh-tw/windows/wsl/install-win10) https://medium.com/swlh/wsl-2-docker-edge-tech-preview-native-linux-containers-w-o-emulation-b41667e6dbef https://www.huanlintalk.com/2020/02/wsl-2-installation.html?m=1 https://www.netadmin.com.tw/netadmin/zh-tw/technology/163029C8DF994FE9BD51678CA04A5B42 [tmux 教學](https://mropengate.blogspot.com/2017/12/tmux.html?m=1) ###### tags: `setup`