# Git 常用指令 ###### tags: `常用指令` ## 基本指令 * 將路徑變為 git repositories (啟動 git) ``` git init ``` * 將檔案加入 git ``` git add <file name> ``` * 查看 git add 狀況 ``` git status ``` * 取消 git add ``` git reset <file name> ``` * 添加 commit ``` # 需先完成 git add 才執行 git commit -m <messages> ``` * 查看 git 記錄 ``` # 列出 git commit 的記錄 git log ``` * 回復至特定版本 ``` git reset --hard <commit id> ``` * 回復至上一版本 ``` git reset main^ ``` * 刪除檔案 ``` git rm <file name> git rm -r <folder name> ``` ## 與 github repo 連結 1. 先在 github 開好新的 repository (repo),並複製 git 網址 2. 將當前路徑與 repo 做連結 ``` git remote add origin <repo url> ``` 3. 查看目前連結的 repo ``` git remote -v ``` 4. git add & git commit 完之後,推送至 repo ``` git add <file name> git commit -m "messages" git push -u origin <branch name> ``` * 查看目前的 branch ``` git branch ``` * 切換 brach ``` git checkout <branch name> ``` * 變更連結的 repo ``` git remote set-url origin <repo url> ``` * 刪除 repo 的檔案 ``` git rm <file name> # 刪除本地端及 repo 的檔案 git rm <file name> --cached # 不刪除本地端的檔案 git commit -m "messages" git push -u origin <branch name> ``` ### Github 下載 因為現在 Github 對 HTTPs 要求變高, git clone 需要 token, 所以改用 SSH 來 git clone 1. 建立公私鑰 ``` ssh-keygen cd .ssh ls --- id_rsa #私鑰 id_rsa.pub #公鑰 --- cat id_rsa.pub # 將內容複製到 Github >> Settings >> SSH and GPG keys >> SSH keys # 下載程式碼 git clone git@github.com:HuangDeneil/pipeline.git