Upgrade to Pro — share decks privately, control downloads, hide ads and more …

你應該要把沒用的 commit 壓扁 @ 微程式 2019/05/23

你應該要把沒用的 commit 壓扁 @ 微程式 2019/05/23

本次分享會著重介紹於 git-hubflow 及 git-labflow ,當日分享內容如下:

1. 讓 git 的歷史記錄更乾淨
2. 移除不必要的 commit
3. 確保功能性分支合併前必須 rebase 到最新的 master 分支
4. 利用 Merge Request 來進行非同步 Code Review
5. 如果有寫測試時,確保合併回 master 前必須跑過測試

適合對象:對 git 有基礎之工程師

戴均民

May 23, 2019
Tweet

More Decks by 戴均民

Other Decks in Programming

Transcript

  1. 你應該要把 你應該要把 沒⽤的 commit 壓扁 沒⽤的 commit 壓扁 微程式 /

    研一 / DevOps / 戴均民 微程式 / 研一 / DevOps / 戴均民 投影片:https://ppt.cc/fNqqLx 投影片:https://ppt.cc/fNqqLx 1 1
  2. 3 3

  3. 建立⼀個空的 git repo 建立⼀個空的 git repo mkdir test mkdir test

    cd test cd test git init git init echo "Hello, ${USER}" >> master.txt echo "Hello, ${USER}" >> master.txt git add . git add . git commit -m "1st" git commit -m "1st" 7 7
  4. 8 8

  5. 於 git repo 中新增 remote 於 git repo 中新增 remote

    # # 請改成自己的專案網址 請改成自己的專案網址 git remote add origin [email protected]:jimmy.dai/git git remote add origin [email protected]:jimmy.dai/git git push -u origin master git push -u origin master 10 10
  6. 建立功能 A 建立功能 A git checkout -b feature/a master git

    checkout -b feature/a master echo "This is feature A" >> featureA.txt echo "This is feature A" >> featureA.txt git add . git add . git commit -m "2nd" git commit -m "2nd" git push -u origin feature/a git push -u origin feature/a echo "這是功能 A" >> featureA.txt echo "這是功能 A" >> featureA.txt git add . git add . git commit -m "3rd" git commit -m "3rd" git push git push 13 13
  7. 建立功能 B 建立功能 B git checkout -b feature/b master git

    checkout -b feature/b master echo "This is feature B" >> featureB.txt echo "This is feature B" >> featureB.txt git add . git add . git commit -m "4th" git commit -m "4th" git push -u origin feature/b git push -u origin feature/b echo "這是功能 B" >> featureB.txt echo "這是功能 B" >> featureB.txt git add . git add . git commit -m "5th" git commit -m "5th" git push git push 14 14
  8. 建立功能 C 建立功能 C git checkout -b feature/c master git

    checkout -b feature/c master echo "This is feature C" >> featureC.txt echo "This is feature C" >> featureC.txt git add . git add . git commit -m "6th" git commit -m "6th" git push -u origin feature/c git push -u origin feature/c echo "這是功能 C" >> featureC.txt echo "這是功能 C" >> featureC.txt git add . git add . git commit -m "7th" git commit -m "7th" git push git push 15 15
  9. Rebase and Squash Rebase and Squash 將分⽀ rebase 到最新的 master

    將分⽀ rebase 到最新的 master 保留有意義的 commit 保留有意義的 commit 27 27
  10. Rebase and Squash Rebase and Squash 將分⽀ rebase 到最新的 master

    將分⽀ rebase 到最新的 master 保留有意義的 commit 保留有意義的 commit 把 commit 訊息寫的更好 把 commit 訊息寫的更好 27 27
  11. Rebase and Squash Rebase and Squash 將分⽀ rebase 到最新的 master

    將分⽀ rebase 到最新的 master 保留有意義的 commit 保留有意義的 commit 把 commit 訊息寫的更好 把 commit 訊息寫的更好 git fetch --all git fetch --all git checkout feature/a git checkout feature/a git rebase -i origin/master git rebase -i origin/master 27 27
  12. git rebase -i git rebase -i 1. pick: 要使⽤這個 commit

    pick: 要使⽤這個 commit 2. reword: 要修改 commit 訊息 reword: 要修改 commit 訊息 29 29
  13. git rebase -i git rebase -i 1. pick: 要使⽤這個 commit

    pick: 要使⽤這個 commit 2. reword: 要修改 commit 訊息 reword: 要修改 commit 訊息 3. squash: 要把這個 commit 合併到 squash: 要把這個 commit 合併到 前⼀個 commit 中 前⼀個 commit 中 29 29
  14. git rebase -i git rebase -i 1. pick: 要使⽤這個 commit

    pick: 要使⽤這個 commit 2. reword: 要修改 commit 訊息 reword: 要修改 commit 訊息 3. squash: 要把這個 commit 合併到 squash: 要把這個 commit 合併到 前⼀個 commit 中 前⼀個 commit 中 4. fixup: 要把這個 commit 合併到 fixup: 要把這個 commit 合併到 前⼀個 commit 中,但是不保留訊息 前⼀個 commit 中,但是不保留訊息 29 29
  15. git rebase -i git rebase -i 1. pick: 要使⽤這個 commit

    pick: 要使⽤這個 commit 2. reword: 要修改 commit 訊息 reword: 要修改 commit 訊息 3. squash: 要把這個 commit 合併到 squash: 要把這個 commit 合併到 前⼀個 commit 中 前⼀個 commit 中 4. fixup: 要把這個 commit 合併到 fixup: 要把這個 commit 合併到 前⼀個 commit 中,但是不保留訊息 前⼀個 commit 中,但是不保留訊息 5. drop: 把這個 commit 丟棄 drop: 把這個 commit 丟棄 29 29
  16. 將 rebase 後的結果 將 rebase 後的結果 push 到 GitLab 上⾯

    push 到 GitLab 上⾯ ( (--force --force) ) git status # 先確認自己在正確的分支 git status # 先確認自己在正確的分支 git push -f git push -f 34 34
  17. 將 rebase 後的結果 將 rebase 後的結果 push 到 GitLab 上⾯

    push 到 GitLab 上⾯ ( (--force --force) ) 使⽤ 使⽤ git push --force git push --force 務必再次檢查 務必再次檢查 git status # 先確認自己在正確的分支 git status # 先確認自己在正確的分支 git push -f git push -f 34 34
  18. 然後本地端需要 fetch 然後本地端需要 fetch # # 可以加上 --prune 同時刪除遠端沒有的分支(選用) 可以加上

    --prune 同時刪除遠端沒有的分支(選用) git fetch --all git fetch --all 38 38
  19. 把開發中的分⽀移植到最新的 master 上 把開發中的分⽀移植到最新的 master 上 git checkout feature/b git

    checkout feature/b git rebase master git rebase master git checkout feature/c git checkout feature/c git rebase master git rebase master 43 43
  20. 然後把移植的分⽀推送到遠端 然後把移植的分⽀推送到遠端 git checkout feature/b git checkout feature/b git push

    -f git push -f git checkout feature/c git checkout feature/c git push -f git push -f 45 45
  21. 然後把移植的分⽀推送到遠端 然後把移植的分⽀推送到遠端 使⽤ 使⽤ git push --force git push --force

    務必再次檢查 務必再次檢查 git checkout feature/b git checkout feature/b git push -f git push -f git checkout feature/c git checkout feature/c git push -f git push -f 45 45
  22. 練習時間 練習時間 投影片: 投影片: 練習把 練習把 feature/b feature/b 和 和

    feature/c feature/c 也合併 也合併 https://ppt.cc/fNqqLx https://ppt.cc/fNqqLx 47 47
  23. Protect Branch Protect Branch 避免主要的分⽀被 force push 避免主要的分⽀被 force push

    只允許特定的成員 push 及 Merge 只允許特定的成員 push 及 Merge 55 55
  24. 使⽤ non fast forward ( 使⽤ non fast forward (--no-ff

    --no-ff) ) 的⽅式來 Merge 的⽅式來 Merge 但是僅在能夠透過 fast forward ( 但是僅在能夠透過 fast forward (--ff --ff) ) 時才允許 Merge 時才允許 Merge 60 60
  25. 參考資料 參考資料 其實這份簡報有⼀些 GitLab Flow 的內容沒有提 其實這份簡報有⼀些 GitLab Flow 的內容沒有提

    到:如 production 分⽀,這裡附上⼀些參考連 到:如 production 分⽀,這裡附上⼀些參考連 結,裡⾯有更詳細的說明。 結,裡⾯有更詳細的說明。 Git ⼯作流程 | 阮⼀峰的網絡⽇誌 Git ⼯作流程 | 阮⼀峰的網絡⽇誌 Understanding the GitHub Flow Understanding the GitHub Flow GitLab Flow GitLab Flow Introduction to GitLab Flow Introduction to GitLab Flow 66 66