
إمتى تعمل merge وإمتى rebase؟ نفس الكود، history مختلف، وقاعدة متتكسرش
- كاتب موثّق
محتويات المقال
coupons. وقبل الـ review، سارة عايزة history نضيف، فبتعمل rebase للـ branch على main. والـ Git بيرفض الـ push، فبتزوّد --force. وتاني يوم الصبح عمر يعمل pull، والـ log عنده يطلّع "Add coupon field" مرتين، و "Validate coupon codes" مرتين، و merge commit هو ماكانش ناوي يعملها. مفيش كود ضاع، بس الـ branch بقت بتحكي نفس الحكاية مرتين، وكل reviewer بيسأل أنهي نسخة فيهم الأصلية.
الفكرة الغلط الشائعة
--force بيحل الموضوع.
اللي بيحصل فعلًا
الـ merge بيزوّد commit واحدة ومش بيغيّر حاجة تانية
git merge بتوصفه: Git بياخد تغييرات الـ branch التانية من ساعة ما اتفرقوا، ويسجّل النتيجة "in a new commit along with the names of the two parent commits." والـ دي هي الحاجة الجديدة الوحيدة. وكل commit موجودة بتفضل بنفس الـ ID، فأي حد عنده نسخة منها مش بيتأثر.
الـ rebase بيكتب commits جديدة
نفس الـ snapshot، و commits مختلفة
git log --oneline main..coupons# e8ea870 Validate coupon codes# 8289bcb Add coupon fieldgit switch -q -c try-merge coupons && git merge -q --no-edit maingit switch -q -c try-rebase coupons && git rebase -q maingit log --no-walk --format='%h %t %s' try-merge try-rebase# 834ad14 0bf12b3 Validate coupon codes# c923a93 0bf12b3 Merge branch 'main' into try-merge%t، هو الـ tree: الـ snapshot بتاعة كل الملفات. وقيمته 0bf12b3 في الاتنين، يعني الكود واحد. أما العمود الأول فلأ: "Validate coupon codes" كانت e8ea870 وبقت 834ad14. وكتاب Pro Git بيقولها بالظبط: "it's only the history that is different."
الـ commits المشتركة هي اللي بتخلّي الفرق يفرق
git rebase صريحة: "Rebasing (or any other form of rewriting) a branch that others have based work on is a bad idea: anyone downstream of it is forced to manually fix their history." وكتاب Pro Git بيلخّصها في قاعدة واحدة: "Do not rebase commits that exist outside your repository and that people may have based work on."
الحل: اعمل merge للمشترك، و rebase للي بتاعك
# Sara ran: git rebase main, then git push --forcegit pull --no-rebase # Omar, with one unpushed commit# From ../shop# + b3086e8...d707e50 coupons -> origin/coupons (forced update)# Merge made by the 'ort' strategy.git log --oneline --graph# * f7d87bc Merge branch 'coupons' of ../shop into coupons# |\# | * d707e50 Validate coupon codes# | * 9356d40 Add coupon field# | * 4680f97 Add shipping to total# * | 14e1e70 Show coupon errors# * | b3086e8 Validate coupon codes# * | 2abf6f9 Add coupon field# |/# * 86434d1 Add checkout# Sara ran: git merge --no-edit main, then git pushgit pull --rebase # Omar replays only his unpushed commit# From ../shop# 423afde..e9367a2 coupons -> origin/coupons# Successfully rebased and updated refs/heads/coupons.git log --oneline --graph# * 0435bf2 Show coupon errors# * e9367a2 Merge branch 'main' into coupons# |\# | * 65f5f84 Add shipping to total# * | 423afde Validate coupon codes# * | 42d4aa8 Add coupon field# |/# * fc07af2 Add checkoutgit push# To ../shop.git# e9367a2..0435bf2 coupons -> couponsgit push العادي بتاع سارة كان اترفض بـ non-fast-forward قبل ما تعمل force. والـ pull بتاع عمر جاب الـ branch اللي اتعاد كتابتها، ولقى الـ history بتاعه وبتاع الـ remote متفرقين، فعمل merge، وبقت كل commit من بتوع سارة موجودة مرتين: b3086e8 و d707e50 الاتنين "Validate coupon codes". وفي التانية، سارة عملت merge لـ main بدل كده، فالـ commits بتاعتها فضلت بنفس الـ IDs والـ push العادي اشتغل. وعمر عمل replay للـ commit بتاعته اللي لسه ماتعملهاش push بس، بـ git pull --rebase، والـ push بتاعه كمان ماحتاجش force.
تطبقها إزاي في مشروعك
اتفقوا على أنهي branches مشتركة الـ main، وأي branch أكتر من شخص بيعمل عليها push، دي مشتركة. اكتبوا ده، واقفلوا الـ force push علىmainمن إعدادات الـ branches في الـ Git host بتاعكم.ادمج الـ branches المشتركة بـ merge عشان تجيب mainجوه feature branch مشتركة، اعملgit merge main. بتزوّد commit واحدة ومش بتعيد كتابة أي حاجة عند حد تاني.اعمل rebase بس للـ commits اللي ماخرجتش من جهازك استخدم git pull --rebase، أوgit rebase -iعشان تنضّف قبل أول push. وأول ما الـ commit يتعملها push وممكن حد يكون سحبها، سيبها في حالها.لو لازم تعيد كتابة branch اتعملها push، استخدم الـ lease في branch انت لوحدك اللي بتعمل عليها push، ارفع التعديل بـ git push --force-with-lease، وعمرك ما تستخدم--forceالعادي. هو بيرفض لو الـ remote فيه commits انت لسه ماعملتلهاش fetch.
الخلاصة
الـ merge والـ rebase ممكن يطلّعوا نفس الملفات، والفرق في الـ history بس. الـ merge بيزوّد commit واحدة ليها اتنين parents، وبيسيب كل الـ commits الموجودة زي ما هي. الـ rebase بيستبدل الـ commits بتاعتك بنسخ جديدة ليها IDs جديدة. عمرك ما تعمل rebase لـ commits موجودة بره الـ repo بتاعك وممكن حد يكون بنى شغله عليها. اعمل rebase لشغلك المحلي قبل الـ push، و merge للـ branches المشتركة، واستخدم --force-with-leaseبدل--force.
التعليقات (0)