“Can you please squash your commits together so we get a clean, reversible git history?”.

Teerapong Singthong 👨🏻‍💻
iamgoangle
Published in
4 min readDec 5, 2019
Photo by Headway on Unsplash

ทำไมการทำ squad rebase จึงจำเป็น?

ถ้าทีมเราใหญ่มาก เราทำงานด้วยกันหลายคน การทำ squad rebase workflow ช่วยให้เรารวบ commit message ให้เหลืออันเดียว ก่อน merge เข้า master branch และ ดีกว่านั้นคือ การ revert commit ทำได้ง่าย เพราะ ของได้ถูกรวบรวมเป็นชิ้นเดียวแล้ว

วิธีการทำ

ก่อนเริ่ม squad อยากแชร์ rebase flow ที่ผมทำทุกเช้ากันก่อน ว่าผมทำอย่างไร

ในทุกๆเช้าผมจะทำการ rebase main branch มาใส่ feature branch ที่ผมทำงานอยู่ทุกครั้ง เพื่อให้ branch ผมนั้นตามทัน main branch อยู่เสมอ และ history ของงานผมนั้น อยู่ top ของ main branch ไม่ใช่ผสมมั่วไปหมด

Checkout master branchgit checkout masterPull master branchgit pull origin masterCheckout bug/feature branchgit checkout branchNameRebase from mastergit rebase mastergit push origin branchName --force

จากนั้นผมก็ทำงานในระหว่างวันปกติ

สมมุติผมทำงานอยู่ branch ใดๆก็ตาม ผมทำไปแล้ว 4 commit

ให้สมมุติว่ามันมีมากกว่า 10x commits

ผมพร้อมจะส่ง pull request ไปให้ทีมรีวิวแล้ว แต่…!!! ถ้าหาก pull request ผมนั้น commit history มันมีเยอะมากๆ เช่น เขียนเทสเสร็จหนึ่งชิ้น ก็ commit มันก็จะเยอะพอสมควร และ context คงไม่เกี่ยวกับงานหลักสักเท่าไหร่

และ ที่สำคัญถ้าเกิดพลาดมีบั๊ก การจะ revert โดย cherry pick หลายๆ git revision คงใช้เวลานาน

อันนี้เหตุผลของแต่ละทีมนะ ต้องตกลงกันดีๆ ไม่แนะนำให้ใช้ rebase + squad flow เพราะไม่รู้จักมัน มันมีความอันตรายอยู่ไม่น้อย

อ่ะ หลังจาก รู้ข้อดีของมันแล้ว ผมจะทำการ squad commit ผมแล้ว

rebase squad workflow

ดู commit log ก่อนเป็นอันดับแรก

command
git log --graph --decorate --pretty=oneline --abbrev-commit
output
* 2c1c095 (HEAD -> develop, origin/develop) chore: add test 4 file
* 4452bf1 chore: add test 3 file
* c295abf chore: add test2
* d4c7927 chore: add test1 txt file
* ...
* more than 10x commit ...

ตอนนี้ HEAD ของผมอยู่ที่ 2c1c095 ผมจะทำการรวม commit ส่วนที่เหลือด้านล่างทั้งหมด

command
git rebase -i HEAD~3
output
1 pick c295abf chore: add test2
2 pick 4452bf1 chore: add test 3 file
3 pick 2c1c095 chore: add test 4 file
4
5 # Rebase d4c7927..2c1c095 onto d4c7927 (3 commands)
6 #
7 # Commands:
8 # p, pick = use commit
9 # r, reword = use commit, but edit the commit message
10 # e, edit = use commit, but stop for amending
11 # s, squash = use commit, but meld into previous commit
12 # f, fixup = like "squash", but discard this commit's log message
13 # x, exec = run command (the rest of the line) using shell
14 # d, drop = remove commit
15 #
16 # These lines can be re-ordered; they are executed from top to bottom.
17 #
18 # If you remove a line here THAT COMMIT WILL BE LOST.
19 #
20 # However, if you remove everything, the rebase will be aborted.
21 #
22 # Note that empty commits are commented out

ให้เปลี่ยน pick เป็น squash หรือ s

command
1 s c295abf chore: add test2
2 s 4452bf1 chore: add test 3 file
3 s 2c1c095 chore: add test 4 file
4
5 # Rebase d4c7927..2c1c095 onto d4c7927 (3 commands)
output
1 feat: this is my feature
2
3 # This is a combination of 4 commits.
4 # This is the 1st commit message:
5
6 chore: add test1 txt file
7
8 # This is the commit message #2:
9
10 chore: add test2
11
12 # This is the commit message #3:
13
14 chore: add test 3 file
15
16 # This is the commit message #4:
17
18 chore: add test 4 file

จากนั้นลองดู git log อีกครั้ง

5b9f301 (HEAD -> develop) feat: this is new feature

จะเห็นได้ว่า commit ใหม่ ได้ทำการรวม 4 commit มาเรียบร้อยแล้ว

จากนั้นให้ทำการ push ของเข้า branch อีกครั้ง

command
git push origin branchName --force
output
* 5b9f301 - (HEAD -> develop, origin/develop) feat: this is new feature (7 minutes ago) <Teerapong Singthong>

เมื่อดูภาพรวมของคำสั่ง squad จะช่วยให้ commit ของเราก่อนส่ง pull request นั้น clean ขึ้น คือ history หายไปหมด ฮ่าาา ถูกรวบให้เหลือชิ้นเดียว ซึ่งมันง่ายในตอน revert หากมีปัญหาขึ้นมา แต่ก็ต้องใช้อย่างระมัดระวังด้วย เพราะ history graph มัน สวยก็จริง แต่หายหมดไม่หลงเหลือเลยสำหรับ feature branch นั้นๆ

ทั้งนี้ทั้งนั้นผมแนะนำว่าให้ตกลงกันในทีมก่อนว่า git rebase + squad workflow ทีมต้องการมันไหม อยากให้ main branch ดูแลง่ายขึ้นไหม แต่มันก็ต้องแลกกับความจุกจิกเล็กน้อย ก่อนส่ง pull request นะ

References

--

--

Teerapong Singthong 👨🏻‍💻
iamgoangle

Engineering Manager, ex-Solution Engineering Lead at LINE | Tech | Team Building | System Design | Architecture | SWE | Large Scaling System