# Git & Github ###### tags: `Helper`, `git` ## 設定shh公鑰 此區塊皆於powershell進行,若沒有openssh先依照[此流程](https://learn.microsoft.com/zh-tw/windows-server/administration/openssh/openssh_install_firstuse)下載 1. 產生公鑰 ``` ssh-keygen ``` ![](https://hackmd.io/_uploads/rkZ2w67Hh.png) ``` Your identification has been saved in C:\Users\cow27/.ssh/id_rsa. Your public key has been saved in C:\Users\cow27/.ssh/id_rsa.pub. The key fingerprint is: SHA256:KkY0tqKhA28tKnumFXGyIhA/A43W9BfuyPZ8+G+keMw cow27@DESKTOP-TFNC22L The key's randomart image is: (as image above) ``` 2. 取得公鑰 ``` cat ~/.ssh/ubuntu_id_rsa.pub ``` 3. 加入公鑰至github ![](https://hackmd.io/_uploads/B1dRoaXH3.png) --- ## Git初始設定 (Git Bash) 1. 在資料夾右鍵 >> Git Bash 2. 輸入指令建立使用者名稱 ``` git config --global user.name "franchingkao" git config --global user.email "fran.chingkao@gmail.com" ``` ![](https://hackmd.io/_uploads/BJd0Ja7rh.png) ![](https://hackmd.io/_uploads/SkPwnnmS3.png) --- ## 建立與連結repository 初次建檔或非用git clone載下來的資料,先到github建立新的repository,給待會要push上的資料一個家。 ![](https://hackmd.io/_uploads/SkKwQWeKn.png) ## 連結至github指定repo (remote) 接著,設定連結到你剛才建立的repos,`https://github.com/franchingkao/test.git`這串就在repos中(下圖所示): ``` git remote add origin <https://github.com/franchingkao/test.git> ``` or ``` git remote set-url origin https://YOUR_GITHUB_USER@github.com/YOUR_GITHUB_USER/Repo_name.git ``` ![](https://hackmd.io/_uploads/SkBS4-xt3.png) --- ## 基本操作 (建檔至上傳) 0. 初始化 (init) 在github建立好reporitory後,我們便可以在本地建立資料夾,作為本機撰寫程式的位置。首先在該資料夾開啟Git Bash後,輸入以下指令進行初始化: ``` git init ``` ![](https://hackmd.io/_uploads/rJ8ea2mB2.png) 1. 針對檔案進行動作 (add) ``` git add filename ``` 或加入所有修改的檔案 ``` git add . ``` ![](https://hackmd.io/_uploads/r1MD637rn.png) 2. 版本進度存檔 (commit) 打開文字編輯器進行版本存檔 ``` git commit ``` ![](https://hackmd.io/_uploads/SJokC3XBn.png) 或是直接在CMD中進行版本存檔 ``` git commit -m "state your commit" ``` ![](https://hackmd.io/_uploads/B19EAh7r3.png) > 此刻,因hello1.txt已被存檔,當我們查看git status便不會再看見它。而若我們去修改hello1.txt的內容,再次輸入git status時便會發現它又變成Untracked files了。 > ![](https://hackmd.io/_uploads/HyLDAnmrh.png) 3. 查看檔案狀態 (status) 在新增、刪除、修改檔案後,git會監控檔案是否有變動,可以輸入指令查看哪些檔案的當前狀況,也能知道哪些檔案已經`git add`。 ``` git status ``` ![](https://hackmd.io/_uploads/SyZET3XHn.png) 4. 上傳版本至github (push) `-u`代表將此設定為預設,下一次再push時不用特別指定`<remote name>` `<branch name>`就會直接上傳到一開始預設的分支中。 ``` git push -u <remote name> <branch name> ``` ``` git push <branch name> ``` ![](https://hackmd.io/_uploads/ByZRQRQSh.png) 完成!push成功後會看到github上出現剛剛上傳的檔案以及comment內容和時間,這些都會記錄在github方便未來進行版本管理。 ![](https://hackmd.io/_uploads/BJfxVRmH2.png) 5. 刪除檔案 ``` git rm FILE_NAME ``` 或復原被刪除檔案 ``` rm FILE_NAME git checkout -- FILE_NAME ``` 6. 修改檔案名稱 ``` mv OLD_FILE_NAME NEW_FILE_NAME ``` 7. 移動檔案位置 ``` mv OLD_PATH NEW_PATH ``` 8. 退回版本 `--hard`表示丟棄add和commit下的指令,退回到該版本;也能改成`--soft`,表示只丟棄commit內容。 ``` git reset --hard HEAD~1 #上一個版本 git reset --hard HEAD~2 #上上一個版本 ``` --- 完成github基本操作!以後不用再自己到github上亂加檔案了XD 之後再去研究更多功能: - [Git-Tutorials 基本使用教學 📝](https://github.com/twtrubiks/Git-Tutorials) - [Git Commit Message 格式與規範整理](https://hackmd.io/@dh46tw/S1NPMsy5L) --- ## Reference * [github基本教學 - 從無到有](https://www.youtube.com/watch?v=py3n6gF5Y00) * [Git 初始設定](https://ithelp.ithome.com.tw/articles/10240965) * [教學 產生SSH Key並且透過KEY進行免密碼登入](https://xenby.com/b/220-%E6%95%99%E5%AD%B8-%E7%94%A2%E7%94%9Fssh-key%E4%B8%A6%E4%B8%94%E9%80%8F%E9%81%8Ekey%E9%80%B2%E8%A1%8C%E5%85%8D%E5%AF%86%E7%A2%BC%E7%99%BB%E5%85%A5) * [設定 Github SSH 金鑰 feat. Github SSH、HTTPS 的差異](https://ithelp.ithome.com.tw/articles/10205988) * [windows的CMD無法使用cat指令?](https://blog.csdn.net/qq_36229552/article/details/117448013?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-117448013-blog-87977107.235%5Ev36%5Epc_relevant_anti_vip&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-117448013-blog-87977107.235%5Ev36%5Epc_relevant_anti_vip&utm_relevant_index=1)