# Git Tutorial ## Start a git project ### git init 初始化 ![](https://hackmd.io/_uploads/ryxTr6eTh.png) ### git clone 複製下載 `git clone <網址>` ## About git remote ### git push & pull 上傳/下載 遠端 ![](https://hackmd.io/_uploads/BJTiv0lph.png) ![](https://hackmd.io/_uploads/rJ7k_Cgp3.png) ## About git commit ### git add 增加檔案到暫存區 ![](https://hackmd.io/_uploads/rkgGpRla3.png) ### git rm 從暫存區移除檔案 ![](https://hackmd.io/_uploads/B12veyW6h.png) ### git log 顯示提交變更 ### git status 顯示變更檔案的狀態 ### git commit ![](https://hackmd.io/_uploads/Sk2Reyb63.png) ### git revert 移除 commit,在需要移除的 commit 按右鍵 ![](https://hackmd.io/_uploads/SkCoUJZp2.png) ## About git branch ## git switch 切換分支 ![](https://hackmd.io/_uploads/HJQf6JbT3.png) ## git branch 建立分支 ![](https://hackmd.io/_uploads/rkoL6y-pn.png) ## git merge 分支合併 ![](https://hackmd.io/_uploads/rk_hRyWa3.png) ### merge 種類 * merge (fast-forward merge) 預設 merge 選項,會將兩分支的 commit 按時間戳記排序並整合成同分支 * merge without fast-forwarding 會保留兩分支的 commit ,並新增 merge 的 commit 在合成分支上 ## git tag 建立標籤 ![](https://hackmd.io/_uploads/r1lEqGGph.png) # git for vscode ## github ssh setting 1. 輸入 `ssh-keygen -t rsa -b 4096 -C "your_email@example.com"` 成功後會顯示 ``` Generating public/private rsa key pair. Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter] Enter passphrase (empty for no passphrase): [Type a passphrase] Enter same passphrase again: [Type passphrase again] ``` 之後就是輸入兩次密碼 2. 輸入 `cat ~/.ssh/id_rsa.pub` 3. 複製整串印出來的東西 4. 到 github/gitlab 網站 * github 1. settings->SSH and GPG kyes 2. 按 New ssh key 3. 在 key 貼上複製的東西 4. add SSH key * gitlab 1. Edit profile 2. SSH keys 3. 按 Add new key 4. 在 key 貼上複製的東西 5. Add key 5. 測試 輸入 `ssh -T git@gitlab.com`,應該會顯示 ``` Hi <ACCOUNT_NAME>! You've successfully authenticated, but GitHub does not provide shell access. ``` ## github/gitlab auto login setting 1. 開啟 git bash terminal 1. 在 terminal 右手邊 **加號**右邊的向下箭頭 2. 點 `git bash` 2. 輸入 `eval "$(ssh-agent -s)"` 3. `ssh-add ~/.ssh/id_rsa` ## 設定預設帳號 1. `git config --global user.name "USER_NAME"` 2. `git config --global user.email johndoe@example.com`