# github ###### tags: `額外資料` --- <!-- {%hackmd BJrTq20hE %} --> ## 本地端設定 在需要上傳到github的目錄下做下面的步驟 **(如已安裝git則跳過1.)** ### 1. 安裝git ```git= sudo apt install -y git ``` ### 2. 新增.git檔 ```git= git init ``` 建立完成後可透過`la`指令查看目錄中是否存在.git * #### 設定使用者資訊 :::info 每次使用git時,會記錄是哪個使用者操作此次的版本更新,故需要先設定本地端的使用者資訊,而使用者資訊回儲存在`git config`內 下面填入使用的github帳號資料: ```git= git config --global user.name "您的姓名" git config --global user.email "您的Email" ``` 設定完後可以使用`git config --list`查看使用者資訊 ::: ### 3. SSH key :::success * #### Check for existing SSH keys ```git= ll~/.ssh ``` 如果找不到檔案就是沒有SSH key * #### 生成SSH key 如不設密碼就連按三次enter 最後會出現一幅可愛的畫 ```git= ssh-keygen -t ed25519 -C "email@xxx.com" ``` * #### add to the ssh-agent ```git= eval $(ssh-agent -s) ssh-add ~/.ssh/id_ed25519 ``` * #### 顯示生成的SSH public key ```git= cat ~/.ssh/id_ed25519.pub ``` * #### 複製SSH public key到github (github>>setting>>Public profile>>SSH and GPG keys) 照上面的路徑移動後會看到綠色的按鈕NEW SSH key 添加進去 ::: ### 4. 建立新的repo(有repo就可以跳到5.) * ##### 在github上新增repo 進入github後點擊頭像,選擇your repositories,點擊綠色的新增選項 * ##### git檔設定 :::info 回到本地端,按照下列步驟設定.git檔 ```git= git init //初始化 會清空.git內的參數 git add . //將所有檔案添加進commit git status //查看當前狀態 git commit -m "first commit" //寫入commit資訊,類似為這次的push加上註解 git branch -M master //新增branch "-M"的意思是新增後並移動到這個branch下 "master"可自行命名 //"master"一般為最穩定的版本 git remote add origin git@github.com:xangusx/test.git //添加路徑 "origin"可自己命名代表後面那個路徑 git push -u origin master //推上github中"main"這個branch ``` ::: ### 5. push到已經存在的remote repository 須注意本地目錄底下已經存在.git檔 如沒有.git檔[點擊這裡](https://) ```git= git remote add origin git@github.com:...SSH USL git branch //查看當前的branch 如要新增branch則執行下一行 git branch -M "branch_name" git push -u origin master ``` --- ## 更新版本push&pull 後續需要更新版本時會用到的git指令 ### push ```git= git add . git commit -m "version" git push "repo_var" "branch" ``` ### pull ```git= git fetch "branch" git merge "repo_var"/"branch" git pull --rebase git reset --hard //放棄本地端版本 git pull ``` --- ## Clone檔案下來 ### 1. clone全部檔案 ```git= git clone "https://..." ``` ### 2. clone部分資料夾 :::info 1. 移動到想要下載的資料夾的位置,複製瀏覽器的地址 (https://.../github-direction/../目標檔案) 2. 將網址改成:https://.../github-direction/trunk/目標檔案 3. clone到裝置 ```git= svn co https://.../github-direction/trunk/目標檔案 ``` 參考網站:https://www.twblogs.net/a/5cc8c264bd9eee1ac2ed9356 :::