# Git 小技巧 <!-- Put the link to this slide here so people can follow --> slide: https://hackmd.io/@harunanase/r123ToQQv?type=slide#/ --- # 簡介 ---- 大家都熟了一些基本操作吧(? 那我們就來談談一些容易忘記的概念 與平常會遇到的一些狀況吧! --- ## 觀念 1 commit ---- ### Commit? 提交。 ---- ### 什麼時候可以提交? 完成**一個**功能,確定此功能可正常運行,所做出的 **「提交」** 的這個動作。 --- ![](https://i.imgur.com/n68PPWT.png =800x500) [src: https://gitbook.tw/chapters/using-git/working-staging-and-repository.html](https://gitbook.tw/chapters/using-git/working-staging-and-repository.html) ---- ### 工作目錄 <img src=https://i.imgur.com/cL6zBSz.png align="center" width="800" height="600"> ---- ### 暫存區 <img src=https://i.imgur.com/UMo4LLC.png align="center" width="800" height="600"> ---- ### 儲存庫 ![](https://i.imgur.com/C2hYVnS.png) --- ### 為什麼要二段式? 好麻煩? 1. Double check 2. 方便分類 並不是每次你改的東西都要 commit 上去 --- ## 觀念 2 local / remote ![](https://i.imgur.com/I1qAeGN.png) [src: https://backlog.com/git-tutorial/syncing-repositories/](https://backlog.com/git-tutorial/syncing-repositories/) ---- local: 你的電腦 remote: [http://192.168.0.119:9999/](http://192.168.0.119:9999/) --- ## 狀況一 剛剛的 commit 有錯,怎辦? 能重新 commit 嗎? ---- ### 當然可以,哪次不可以? keyword: **reset** ---- ### `ab8b9c1` 是個錯誤的 commit ![](https://i.imgur.com/LNnDMUL.png) 能回到 `795e77a` 嗎? ---- ### 回到 `795e77a` ![](https://i.imgur.com/JP1fUhS.png) ---- ### reset 有三種模式 <table> <tr> <td> <img src=https://i.imgur.com/WKMA5NA.png> </td> <td> <p> → </p> </td> <td> <img src=https://i.imgur.com/3WbgQDH.png> </td> </tr> </table> | 模式 | soft | mixed(default) | hard | | :---: | :----: | :---------------: | :---: | | 工作目錄 | 保留 | 保留 | 丟掉 | | 暫存區 | 保留 | 丟掉 | 丟掉 | ---- ### soft 情況 ![](https://i.imgur.com/vHC0qYz.png) ![](https://i.imgur.com/Uv5KmGS.png) ---- ### mixed (預設) 情況 ![](https://i.imgur.com/vHC0qYz.png) ![](https://i.imgur.com/ZoFkG6v.png) ---- ### hard 情況 ![](https://i.imgur.com/CO1RMsz.png) ![](https://i.imgur.com/MIdY19f.png) --- ## 觀念 3 所謂的 git 只是一堆修正紀錄的串聯 ---- ![](https://i.imgur.com/JCmUfsl.png) [src: https://www.slideshare.net/KatrinaSylorMiller/giting-out-of-your-git-messes](https://www.slideshare.net/KatrinaSylorMiller/giting-out-of-your-git-messes) ---- ### Branch 其實就是一個指標(貼紙) ![](https://i.imgur.com/Q9ztrnv.png) ---- ### Recall: reset 你在 master branch 使用 `reset`, 只是在告訴 git 你要把 "master" 這個貼紙 貼到哪裡而已 ![](https://i.imgur.com/ui9pLlT.png) ↓ ![](https://i.imgur.com/eXR0ypu.png) --- ## 觀念 4 checkout 與 reset 的差別 ---- ### 在這之前 先來講講何謂 `HEAD` ? ---- ### HEAD 有兩種 1. HEAD 當前你所在的 commit (就是你現在在的位置) 2. origin/HEAD 遠端的 default branch ---- ### HEAD 的意義 `HEAD` 其實也只是一個貼紙, 表示當前你在哪一個 commit, 也就是在哪一個 branch 上。 ![](https://i.imgur.com/AYLrOOY.png) ![](https://i.imgur.com/z82tMCi.png) --- ## checkout 1. switch to specific commit (現在只討論這點) 2. restore working tree files --- ### checkout - 切換到特定 commit 實現方法: 移動 HEAD。 把 HEAD 貼紙貼到你想要移動到的 commit 即可 ---- ### Ex: 移動到最初的 commit (`6c315fa`) ![](https://i.imgur.com/HwhASzj.png) ↓ ![](https://i.imgur.com/R9tfK4b.png) --- ## checkout 跟 reset 差別 checkout 只移動 HEAD 貼紙; 而 reset 會移動 HEAD 跟當前所在 brach 的貼紙。 ---- ### 舉例 ![](https://i.imgur.com/AHoOV8T.png) 1. `$ git checkout 6c315fa` ![](https://i.imgur.com/oP7gFR0.png) 2. `$ git reset 6c315fa` ![](https://i.imgur.com/BTZiSqm.png) ---- ### 小結 checkout 不會動到「儲存庫」資訊,而 reset 會, 因為 reset 比起 checkout, 多移動了一個貼紙(就是上個例子中的master貼紙)。 所以當我們 commit 做錯要還原時, 要用的是 reset 而不是 checkout。 ---- ### 看起來 reset 很恐怖,所以... `reset` 後,我又後悔了,救的回來嗎? ---- ### 當然可以,哪次不可以 就算是使用 `--hard` 參數也救得回來 ---- ### `$ git reflog` ![](https://i.imgur.com/ejgR3QP.png) [延伸閱讀](https://gitbook.tw/chapters/using-git/restore-hard-reset-commit.html) --- ## 狀況二 東西做到一半,臨時要切換到別的任務 例: 我在 RD branch 開發到一半, 突然收到 Prod 分支有問題的消息, 但是現在東西才做到一半, 不想 commit,該怎麼辦? ---- ### 切換分支時,是不是常常遇到此情況? ![](https://i.imgur.com/2JRQ9pn.png) <table> <tr> <td> <img src=https://i.imgur.com/JaRCrZv.png> </td> <td> <img src=https://i.imgur.com/udgyOCo.png> </td> </tr> </table> ---- ### 解決方法 1. commit 2. **stash** 若不想 commit,這時候就可以選擇 stash ---- ### stash 用法 ![](https://i.imgur.com/uDkaOWI.png =1024x170) <table> <tr> <td> <img src=https://i.imgur.com/ZMO2UD8.png> </td> <td> <img src=https://i.imgur.com/FERfrxj.png> </td> </tr> </table> ---- ### stash 使用時機 - 當你 local 有修改時,不想捨棄, 但又還不到能 commit 的程度時, 就可使用 stash 來把你所做的修改 「暫存」至另一個空間, - 等到其他事情處理完後, 再把你所做的修改還原回來 --- ## 觀念 5 commit message 怎麼寫? [推薦文章](https://blog.louie.lu/2017/03/21/%E5%A6%82%E4%BD%95%E5%AF%AB%E4%B8%80%E5%80%8B-git-commit-message/) --- ## Q&A --- ## Thanks!
{"metaMigratedAt":"2023-06-15T12:05:41.894Z","metaMigratedFrom":"YAML","title":"Git 小技巧","breaks":true,"description":"恆為公司內部用 Git 小技巧分享","slideOptions":"{\"themem\":\"Moon\",\"transition\":\"slide\",\"rtl\":true}","contributors":"[{\"id\":\"d5631a87-d157-4ab3-a80b-471d29abd049\",\"add\":7605,\"del\":2731}]"}
    603 views