Skip to content
GitHub

ကိုယ့် Story ကို သပ်ရပ်အောင် ပြင်ဆင်ခြင်း - git rebase အကြောင်း

git rebase ဆိုတာ ဘာလဲ? (ကိုယ့် Story ကို ပြန်ရေးခြင်း)

Section titled “git rebase ဆိုတာ ဘာလဲ? (ကိုယ့် Story ကို ပြန်ရေးခြင်း)”

git rebase ဆိုတာ -

  • Branch တစ်ခုက Commits တွေကို Commit အသစ်တစ်ခုအဖြစ် ပြန်လည် တည်ဆောက်တဲ့ နည်းလမ်းတစ်ခုပါ။
  • ကိုယ့် Branch ရဲ့ စတင်တဲ့နေရာ (Base) ကို ပြောင်းလဲပစ်လိုက်တာ ဖြစ်ပါတယ်။
  • ကိုယ်က အခုမှ တခြားနေရာကနေ အလုပ်စခဲ့သလိုမျိုး ဖြစ်သွားစေပါတယ်။
  • အလွယ်ပြောရရင် Commit History ကို ပြန်လည် ရေးသားလိုက်တာပါ။

အသုံးပြုပုံများ (စတင်လေ့လာသူများအတွက်)

Section titled “အသုံးပြုပုံများ (စတင်လေ့လာသူများအတွက်)”
  1. Local Feature Branch History ကို ရှင်းလင်းခြင်း: Merge မလုပ်ခင်မှာ မှတ်တမ်းများနေတဲ့ Commits တွေကို ပိုအဓိပ္ပာယ်ရှိတဲ့ Commits နည်းနည်းအဖြစ် ပေါင်းစပ်နိုင်ပါတယ်။ (Interactive Rebase သုံးပြီး လုပ်ပါတယ်)။
  2. Parent Branch က Changes တွေကို ကိုယ့် Branch ထဲ ထည့်သွင်းခြင်း: ကိုယ့် Feature Branch ကို အမြဲ Up-to-date ဖြစ်နေအောင် ကူညီပေးပြီး၊ နောက်ဆုံး Merge လုပ်တဲ့အခါ ပိုပြီး သပ်သပ်ရပ်ရပ် ဖြစ်စေပါတယ်။

Rebase (Linear Story) vs. Merge (Preserves Divergence)

Section titled “Rebase (Linear Story) vs. Merge (Preserves Divergence)”

Scenario 1: Feature Branch ကို main က Changes တွေနဲ့ Update လုပ်ခြင်း

Section titled “Scenario 1: Feature Branch ကို main က Changes တွေနဲ့ Update လုပ်ခြင်း”

ပြဿနာ: ကိုယ်က main ကနေ my-feature ဆိုပြီး Branch သစ်ခွဲခဲ့တယ်။ ကိုယ် အလုပ်လုပ်နေတုန်းမှာ main မှာ အရေးကြီးတဲ့ Updates တွေ ဝင်လာတယ်။ ကိုယ်က အဲ့ဒီ Updates တွေကို အခု လက်ရှိ လုပ်နေတဲ့ my-feature ထဲကို သပ်သပ်ရပ်ရပ် ထည့်သွင်းချင်တယ်။

ဘယ်လို လုပ်မလဲ?

1. အရင်ဆုံး main ကို Update လုပ်ပါ

Bash
git switch main
git pull origin main

2. my-feature Branch ကို ပြန်ပြောင်းပါ

Bash
git switch my-feature

3. main ပေါ်မှာ Rebase လုပ်ပါ

Bash
git rebase main

ဘာဖြစ်သွားသလဲ?

Git က my-feature နဲ့ သက်ဆိုင်တဲ့ Commits တွေကို ခဏ ဘေးဖယ်ထားလိုက်ပါတယ်။ အဲ့ဒီနောက် main က Update Commits တွေကို အရင်ယူပြီး၊ စောစောက ဖယ်ထားတဲ့ my-feature ရဲ့ Commits တွေကို main ရဲ့ အပေါ်မှာ တစ်ခုချင်းစီ ပြန်တင်ပေးလိုက်ပါတယ်။

Scenario 2: Interactive Rebase (git rebase -i) ဖြင့် Local Commits များကို ရှင်းလင်းခြင်း

Section titled “Scenario 2: Interactive Rebase (git rebase -i) ဖြင့် Local Commits များကို ရှင်းလင်းခြင်း”

ပြဿနာ: ကိုယ့် Local my-feature Branch ပေါ်မှာ (Push မလုပ်ခင်) "WIP", "fix typo", "another small fix" လိုမျိုး အစီအစဉ်မကျတဲ့ Commits လေးတွေ အများကြီး ရှိနေတယ်။ Pull Request မတင်ခင်မှာ ဒါတွေကို သင့်တော်တဲ့ Commits ၁ ခု၊ ၂ ခုလောက်ပဲ ဖြစ်အောင် ရှင်းလင်းချင်တယ်။

ဘယ်လို လုပ်မလဲ? (Interactive Rebase)

Terminal window
git rebase -i HEAD~3

(ဒါက “လက်ရှိ နေရာကနေ နောက်ဆုံး Commits (၃) ခုကို Interactive Rebase နဲ့ ပြန်ပြင်မယ်” လို့ ဆိုလိုတာပါ)

ဘာဖြစ်သွားသလဲ?

Git က သတ်မှတ်ထားတဲ့ Text Editor ကို Commits စာရင်းနဲ့ ဖွင့်ပေးပါမယ်။ Commit တစ်ခုချင်းစီအတွက် အောက်ပါ လုပ်ဆောင်ချက်တွေကို ရွေးချယ်နိုင်ပါတယ် -

  • pick: Commit ကို ရှိတဲ့အတိုင်း ဆက်သုံးမယ်။
  • reword (r): Commit ကို သုံးမယ်၊ ဒါပေမဲ့ Message ကို ပြင်ရေးမယ်။
  • squash (s): ဒီ Commit ရဲ့ Changes တွေကို အပေါ်က Commit ထဲ ပေါင်းထည့်မယ် (Message ကိုလည်း ပေါင်းစပ်မယ်)။
  • fixup (f): Squash နဲ့ ဆင်တူပေမဲ့၊ ဒီ Commit ရဲ့ Message ကိုတော့ ပယ်ဖျက်လိုက်မယ်။
rebasing
Terminal window
pick 37709fc WIP
pick 3980a2f fix typo # ဒါကို 's' သို့မဟုတ် 'squash' လို့ ပြောင်းပါ
pick f259ca8 actual feature work # 'pick' ကို 'r' သို့မဟုတ် 'reword' လို့ ပြောင်းပါ