# git 小小分享會 ## 大概是 1. 2023/10/6 週五 17:00,會議室是:Newnham K3290,請帶筆電來。 2. 希望兩個小時內講的完。 4. 會夾雜一部分 SFT221 的東西。 5. 可以三四個人一起比較好,整組一起來更好。 6. 會用中文說明。 ## 目標 1. 略懂略懂 git 和 GitHub 2. 看得懂這裡面的笑點 [Git 面試題 by 高見龍](https://gitbook.tw/interview) 3. 成功發一個 PR (應該可以啦?) ## 假如這些都會的話就來純聊天就好 1. 會用 git clone 複製一個 GitHub repo 下來 2. 可以 git push 自己的程式碼上去自己 GitHub 3. 發一個 PR 到別人 GitHub 4. 用過 git rebase ## 事前準備 - git 安裝,CPR101 應該有做過了(吧?) - [YouTube: Fardad Soleimanloo](https://www.youtube.com/playlist?list=PLxB4x6RkylosAh1of4FnX7-g2fk0MUeyc) ## Agenda - (講解+實作)預計時間 - (10 + 0) git 和 GitHub 簡介 - 學習資源 - David Humphrey [YouTube: Learning Git](https://youtube.com/playlist?list=PLJgO3yLojCBOrAXykp92YlyCalb93sx--&si=WGzrLDF3kGmF5Pw8) - 高見龍 [為你自己學 Git](https://gitbook.tw/) - [Git 練習場](https://gitbook.tw/playground#push) - 官方文件 [The entire Pro Git book](https://git-scm.com/book/en/v2) - 視覺化快速上手 - [Learn Git Branching](https://learngitbranching.js.org/?locale=zh_TW) - [Git Cheat Sheet](https://education.github.com/git-cheat-sheet-education.pdf) by GitHub - [六角學院 Git & GitHub 教學手冊](https://w3c.hexschool.com/git/cfdbd310) - [30 天精通 Git 版本控管 作者: 黃保翕 ( Will 保哥 )](https://github.com/doggy8088/Learn-Git-in-30-days/blob/master/zh-tw/README.md) - 整理一下 - [三句話](https://www.ptt.cc/bbs/joke/M.1611225346.A.773.html) - Why? - meme 圖 - [檔案命名法](https://zh-tw.facebook.com/WDTaiwan/posts/1627468490661086:0) - [File naming conventions when you have tons of revisions](https://doctorfreelance.com/file-naming-conventions/) - What is git? - Linus Torvalds - What is GitHub? - ~~找參考答案的地方~~ - ~~工程師交友網站~~ - 微軟近期的收購 - David 筆記 - 是什麼? - **git is a distributed version control system** - git is a content addressable file system - git **pull** before **push** for avoid conflict - pull = fetch + merge - Two kind of merge - Fast forward merge - Force push - Two kind of branch - topic branch - release branch / Long running branch - (10 + 20) 基本 Github 下載上傳 - 軟體 - [git](https://git-scm.com/downloads) - [VS Code](https://code.visualstudio.com/) - 選項1: [Git Graph](https://marketplace.visualstudio.com/items?itemName=mhutchie.git-graph) - 選項2(圖像化功能只能試用七天): [GitLens — Git supercharged](https://marketplace.visualstudio.com/items?itemName=eamodio.gitlens) - 順便介紹一下 VS code 比較的功能 - 示範 repo: [練習 git-practice](https://github.com/MrBearTW/git-practice) - 今天不會用到 - [Sourcetree](https://www.sourcetreeapp.com/) - [gitkraken](https://www.gitkraken.com/) - 新開一個 repo - 建好拉下來 - 建好推上去 - 指令 - git clone repo_URL - git add - git commit - (10 + 30) SFT 怎麼做 - 下面那個方法太難實作,應該先這樣就好 - 開工 - 初始化 - 分工 - 然後? - 衝突 - Set up editor for vscode - `git config --global core.editor "code --wait"` [Set up the editor for vscode](https://docs.github.com/en/get-started/getting-started-with-git/associating-text-editors-with-git) ![](https://hackmd.io/_uploads/ryJCQfTgT.jpg) ![](https://hackmd.io/_uploads/ByCR7zpep.jpg) - (15 + 10) 專業一點 - Rebase - [How to Use Git Rebase – Tutorial for Beginners](https://www.freecodecamp.org/news/how-to-use-git-rebase/) - 這個認識一下就好,有點麻煩就先算了(感謝我 SFT 同學) - [Slides](https://github.com/MrBearTW/git-practice/blob/main/Meeting.pdf) - [StepByStep for git_practice](https://github.com/MrBearTW/git-practice/blob/main/StepByStep.md) - 最後補充一下:SFT 其中一個小項目 - ms4-git-push-stepBystep.pdf - [Git - git-push Documentation](https://git-scm.com/docs/git-push) ## 今天沒講但其實可以做的事情 - 縮寫(應該算是 Linux 功能) - cherry-pick [【狀況題】如果你只想要某個分支的某幾個 Commit?](https://gitbook.tw/chapters/faq/cherry-pick) - squash [【狀況題】把多個 Commit 合併成一個 Commit](https://gitbook.tw/chapters/rewrite-history/merge-multiple-commits-to-one-commit) - 做錯 [Reset、Revert 跟 Rebase 指令有什麼差別?](https://gitbook.tw/chapters/rewrite-history/reset-revert-and-rebase) - 簡單結論是 reset - `git reset HEAD^` - But [【狀況題】剛才的 Commit 後悔了,想要拆掉重做…](https://gitbook.tw/chapters/using-git/reset-commit) - .gitignore - 藏 **Key**,藏 **環境變數 .env**,不上傳 javascript 套件 - [Visualize branches on GitHub](https://stackoverflow.com/questions/27469952/visualize-branches-on-github) - amend 改 commit 內容 - Modify the commit message `git commit --amend -m "new message"` - [也可修改時間](https://xkcoding.com/2019/01/21/modify-git-commit-timestamp.html) - 這是誰寫的? `git blame 檔名` - [【Git】取消追蹤檔案 by 辛西亞.Cynthia ](https://cynthiachuang.github.io/Ignore-Tracked-Files-in-Git/) ## 可能會用到的一些連結 - 大大的 GitHub - [David Humphrey](https://github.com/humphd) - [Aryan Khurana](https://github.com/AryanK1511/) - David Humphrey 結論 1 - Step 1: Fork the repo - Step 2: In remote, add upstream and origin - Step 3: Go to the master branch / `git checkout master `/ `git pull upstream master` - Step 4: Create a new branch / `git checkout -b name-of-branch` - Step 5: Make changes / `git add .` / `git commit -m "message"` / `git push origin name-of-branch` - David Humphrey 結論 2 - `git clone` - `git checkout -b name-of-branch` - `git add .` - `git commit -m "message"` - `git checkout master` - `git pull upstream master` - `git checkout name-of-branch` - `git rebase master` (or `git rebase -i master`) - 參考圖 [A successful Git branching model](https://nvie.com/posts/a-successful-git-branching-model/) - [如何使用 Git? (遠端篇)](https://hackmd.io/@U0TRn8JAT-S9Y8cK9znmxA/SkMkplt1N?print-pdf#/) - [【#Dev】開發者必須知道的 15 個 Git 小技巧](https://www.facebook.com/photo.php?fbid=2591510904194334&set=a.110864892258960&type=3&permPage=1) - [【狀況題】怎麼有時候推不上去…](https://gitbook.tw/chapters/github/fail-to-push) - [ Github 提交是一個「存檔」的概念。雖然不代表一個人的程式技能水準,但是至少代表了:這個人從幾年前就開始投身 Coding 。有許多存檔也是公開的,可以公開檢驗,大部分都是程式競賽相關。他對程式的興趣和水準是有目共睹。](https://m.facebook.com/story.php?story_fbid=pfbid02isJeU3sGNwrcEQckMNCwJtgvqKFXwPN6WPaSxBdRGEk1dXchsL23bETpbhoEqkvCl&id=100063560691696&m_entstream_source=permalink&refid=52&__tn__=-R) - [每天一個 PR](https://youtu.be/xMUjQKn13PA?si=xfvH4uvSgAJHV8Jw) - [GitHub 小商品](https://www.thegithubshop.com/shop-by-category/collectibles)) ## 還有下次的話 1. 研究一下第一次裝的人要怎麼用? 1. 裝 GitLens 就好 1. 示範一次,實做一次 1. 自己上傳下載自己的 1. 從 GitHub 1. 從本機開始 1. -f 會發生什麼恐怖的事情 1. 製造 conflict,才能解? 1. 成功發一個 PR(含 rebase),PR 之後記得 merge