gitHub Tutorial === ###### tags: `Tutorial` git 下載 --- ![](https://hackmd.io/_uploads/rymGcvLNn.png) ![](https://hackmd.io/_uploads/SJsMcPUNn.png) 初始化 git init --- 首先建立一個資料夾 ![](https://hackmd.io/_uploads/HJbw5PLEn.png) 然後開啟資料夾,右鍵找到 Git Bash Here,並用左鍵點擊 ![](https://hackmd.io/_uploads/Syxu5w8V2.png) 輸入 git init,使資料夾變成 git repository ![](https://hackmd.io/_uploads/SyXs9DLE2.png) 查看當前狀態 git status --- ![](https://hackmd.io/_uploads/ryyC5D84n.png) ![](https://hackmd.io/_uploads/SywC5DINn.png) 紅色正方形中是還沒更新的存檔 1.txt 跟 2.txt 使用 git add <檔案名稱> --- ### 第一種方式 新增檔案到 git ![](https://hackmd.io/_uploads/r17XovUE3.png) 再次使用 git status,發現新增檔案 1.txt,並且 2.txt 還未新增 ![](https://hackmd.io/_uploads/r12NsD8Vh.png) 你也可以使用 git add .,把所有未新增檔案都進行新增 ![](https://hackmd.io/_uploads/rydroDLNh.png) ### 第二種方式 使用 vscode,在 Source Control 點選 **+** 號 ![](https://hackmd.io/_uploads/BJnKoDIE2.png) ![](https://hackmd.io/_uploads/BJZ5iv8Nh.png) 保存進度 git commit -m “<存檔名稱>” (git commit類似於遊戲存檔) --- ### 第一種方式 ![](https://hackmd.io/_uploads/Hkz02wIEh.png) ### 第二種方式 把所有檔案都加到 Staged Changes 中 ![](https://hackmd.io/_uploads/BJvgaPIEn.png) 在 Message 輸入要 commit 的名字 ![](https://hackmd.io/_uploads/S1Ed6PIVh.png) 確認目前存取過的檔案 git log(按 q 可退出 git log) --- ![](https://hackmd.io/_uploads/rySj6vLNn.png) 在 git-hub 建立 repository --- 打開你的 git-hub 新增一個 repository ![](https://hackmd.io/_uploads/rkBk1OLN2.png) 設定repository name/設定公開或私人 ![](https://hackmd.io/_uploads/HJNg1_IEn.png) 點選 create repository ![](https://hackmd.io/_uploads/ryWW1_LE3.png) 將檔案與git-hub連接 --- ### git remote add <自訂名稱> <網址> 在git-hub創建repository之後,複製此網址 ![](https://hackmd.io/_uploads/HyBO1dIN3.png) 並輸入進git bash here ![](https://hackmd.io/_uploads/SJltJdU43.png) 上傳雲端 (git-hub) --- ### git push -u <remote名稱><branch名稱> ++如果你是第一次使用會需要設定帳號密碼++ 輸入 git push -u origin master ![](https://hackmd.io/_uploads/r1ink_IV2.png) -u的意思是它會把你的預設設成origin 之後上傳只要輸入git push就好 不需要git push origin master 把add過的檔案unstage回去 --- ### git reset -- <檔案名稱> #### 第一種方式 ![](https://hackmd.io/_uploads/Hyigx_8Eh.png) #### 第二種方式 在Staged Changes點選-號 ![](https://hackmd.io/_uploads/B1Sfe_L4n.png) 回到上一次commit的狀態(Discard Changes) --- ### git checkout -- <檔案名稱> #### 第一種方式 你要把想回到上次commit的檔案unstage ![](https://hackmd.io/_uploads/SyFHx_I4h.png) 輸入git checkout -- <檔案名稱> ![](https://hackmd.io/_uploads/ry2UxdUV2.png) #### 第二種方式 在Changes中點選discard changes ![](https://hackmd.io/_uploads/H18uldIVn.png) ![](https://hackmd.io/_uploads/HkculdU4h.png) 往前退一個commit(會保留修改的部分) --- ### git reset – soft HEAD~1 (依數字決定會退幾個commit) ![](https://hackmd.io/_uploads/HJJxb_8Nn.png) 回到上一個commit(undo last commit) ![](https://hackmd.io/_uploads/Hy_KZO843.png) 就回到上次commit之前 ![](https://hackmd.io/_uploads/rkDEUdLNn.png) ![](https://hackmd.io/_uploads/S1aN8uL42.png) 上次commit的second version就消失了 ![](https://hackmd.io/_uploads/S1qL8_L42.png) 把git-hub的repository抓下來 --- ### git clone <網址> 從git-hub的code欄中,複製網址 ![](https://hackmd.io/_uploads/Sy__L_INn.png) ![](https://hackmd.io/_uploads/SkpdU_LV2.png) 把git-hub上面更新的內容抓下來 --- ### git pull ![](https://hackmd.io/_uploads/Syv58d8N3.png) 若發生衝突(conflict),選擇要用的檔案 ![](https://hackmd.io/_uploads/Bkmi8dLE3.png) 在電腦上建立一個branch --- ### 第一種方式 建立新branch並切換過去 Git checkout -b <branch名稱> ### 第二種方式 在左下角中找到branch master的圖示加以點擊 ![](https://hackmd.io/_uploads/H1Ekw_IN3.png) 選擇create new branch ![](https://hackmd.io/_uploads/SkxxvdIEn.png) 輸入名字即可建立 ![](https://hackmd.io/_uploads/rkpgvuUN2.png) 查看電腦上的branch(按q可以離開) --- ### git branch ![](https://hackmd.io/_uploads/S1tKPdINn.png) 查看所有的branch(包含remote) --- ### git branch -a ![](https://hackmd.io/_uploads/rkknPOLN3.png) 當無法 pull request? --- ![](https://hackmd.io/_uploads/rJXCDu8Vh.png) 把目前分支的起始點移到最新進度 --- ### git rebase <要 rebase 到哪個分支> 先回到你的 master branch 做 git pull,再回到將要 merge 的 branch,做 git rebase master ![](https://hackmd.io/_uploads/Hk16gk5V2.png) (你可能會有衝突要解) 因為你是把兩個分支合併在一起 ![](https://hackmd.io/_uploads/BJelWy9E3.png) 解完衝突之後,add 所有檔案 **git rebase --continue** **解完衝突後繼續rebase(有時候可能要解數次衝突)** 強制把現在電腦上的紀錄 push 到 git-hub 上 --- ### git push -f 切換分支 --- ### git switch <你想要切換到的 branch> 刪除電腦上 branch --- ### git branch -d <branch 名稱> **(如果這個 branch 有推到 git-hub,git-hub 上的不會刪掉)** ![](https://hackmd.io/_uploads/r1Hmfk5Eh.png)