###### tags: `Git` `GitHub` # 交作業方式 & 同步課綱 ## 同步課綱 1. 先把自己目前改的東西全部 commit:`git commit -am “finished”` 2. 切換到 master,`git checkout master` 3. 跟我的 master 同步:git pull https… master - 也就是:`git pull https://github.com/Lidemy/mentor-program-4th.git master` - 或是設定遠端節點 upstream:`git pull upstream master` 4. commit,`git commit -am “update”` 5. push 回 github:`git push origin master` 6. 切換回作業的 branch,`git checkout week1` 7. 繼續寫作業 - 若進入 vim 模式: ![](https://i.imgur.com/KH7s4HK.png) 參考資料:[超簡明 Vim 操作介紹](https://gitbook.tw/chapters/command-line/vim-introduction.html) ## 交作業流程 ### 從 GitHub clone 課綱到本地端 1. 開啟 [GitHub classroom](https://classroom.github.com/a/SbDvk2VA),複製課綱模板到自己的帳號底下 ![clone-1](https://i.imgur.com/UBpEFxx.png) 2. 在 GitHub 點選 `Clone or download` ![clone-2](https://i.imgur.com/3S4xeqG.png) 3. 輸入 `git clone https://github.com/Lidemy/mentor-program-4th-heidiliu2020.git`, 下載檔案到本地端 ![clone-3](https://i.imgur.com/yLDN87s.png) ### 寫作業前,一定要先新增 branch 4. `git branch week1`:建立 branch week1 ![git branch week1](https://i.imgur.com/UDU1YvA.png) 5. `git checkout week1`:切換到 week1,開始寫作業 6. 寫完作業後,若有新增檔案,要先使用 `git add .`:加入版本控制 7. `git commit -am "week1_finish"`:進行 commit 8. `git push origin week1`:將檔案上傳到遠端 GitHub 9. 到 GitHub 查看 branch 頁面,提出 PR(pull request) 10. 複製 PR 連結,到 Lidemy 學習系統上繳交作業 ![學習系統](https://i.imgur.com/C52SZIQ.png) ### 等助教批改完(由助教點選 Merge pull request) 11. GitHub 頁面會顯示 Merged,代表遠端的分支 week1 已合併回 master 12. 回到本地端,輸入 `git checkout master`:切換到 master 13. `git pull origin master`:將遠端更新過的 master 同步回本地端 14. `git branch -d week1`:刪除已合併的分支 week1 15. `git branch -v`:可查看目前在哪個分支 --- ## 同步分支 ``` $ git push origin master --rebase ``` - 回到上一個 commit: ``` $ git commit HEAD^ ``` - 更新 <指定 remote> 底下的分支: ``` $ git fetch <remote name> ``` - 使用 <所有 remote> 底下的分支 ``` $ git fetch --all 同 git remote update ``` - 在 master 同步分支(Merge) ``` $ git pull <origin> <branch name> ``` - 同步分支(Rebase) ``` git pull <origin> <branch name> --rebase ``` 若沒有設定 upstream,就一定要加 <remote name> 跟 <branch name 參考資料: - [消除 Git 同步產生的惱人 Merge branch Commit](https://blog.darkthread.net/blog/vs-git-pull-rebase/) - [同步遠端分支](https://zlargon.gitbooks.io/git-tutorial/content/remote/sync.html) - [【狀況題】怎麼跟上當初 fork 專案的進度?](https://gitbook.tw/chapters/github/syncing-a-fork.html) - [另一種合併方式(使用 rebase)](https://gitbook.tw/chapters/branch/merge-with-rebase.html)