Skip to main content

How to use GIT? GIT ကိုဘယ်လို အသုံးပြုကြမလဲ (Cheat sheet)...

How to use GIT? 

Git အသုံးပြုပုံကို နားလည်ကျွမ်းကျင်ဖို့ GIT ဘယ်လိုအလုပ်လုပ်လဲ ဆိုတာ နည်းနည်းတော့ သိထားဖို့လိုလိမ့်မယ်။  File ရဲ့ statuses တွေနဲ့ file transitions တွေကို သေချာ နားလည်ထားဖို့လိုတယ်။

File status မှာဆိုရင် file တစ်ခုကို git ကဘယ်အချိန်မှာ track စလုပ်လဲ၊ ဘယ်အချိန်မှာ modified, unmodified, staged statuses တွေဖြစ်သွားလဲဆိုတာတွေ နဲ့ File transitions မှာဆိုရင် file တစ်ခုက ဘယ် command run ရင် local repository မှာရှိတယ်။ ဘယ် command run ရင်တော့ remote repository (server) ကိုရောက်သွားလဲ ဆိုတာတွေကို အနည်းငယ်နားလည်ထားဖို့တော့လိုမယ်။


Git File Statuses

Git မှာ file တွေကို track လုပ်နေတဲ့ statuses တွေရှိတယ်။ အဲ့ဒါတွေကတော့ အောက်ပါအတိုင်းဖြစ်တယ်။

  • Untracked
  • Unmodified
  • Modified
  • Staged

Untracked Status


Untracked ဆိုတာက file တစ်ခုက ပထမဆုံး create လုပ်ပြီးတဲအချိန်မှာ git က အဲ့ file ကိုလုံးဝ track မလုပ်ရသေးတဲ့ file တွေက git မှာ untracked အနေနဲ့ရှိနေတယ်။ အဲ့ဒါကို git ကို စပြီး track လုပ်စေချင်တယ်ဆိုင်တော့ git add <file> command နဲ့ git ကို စ track လုပ်ခိုင်းလို့ရတယ်။ အဲ့လို git add လုပ်လိုက်တာနဲ့ status က staged အဖြစ်ပြောင်းသွားတယ်။

Unmodified

Unmodified ကတော့ရိုးရှင်းတယ်။ file တစ်ခုက repository မှာရှိပြီးသားဖြစ်တယ်။ အဲ့ဒါကို working directoryမှာလဲ​ဘာမှ ပြင်ထားတာတွေမရှိဘူး။

Modified

Modified ကလည်း ရိုးရှင်းတယ်။ file တစ်ခုက repository မှာရှိပြီးသားဖြစ်တယ်။ ဒါပေမဲ့ working directory မှာတော့ တစ်ခုခု ပြင်ထားတာရှိတယ်။

Staged

Staged ကတော့ working directory မှာ modified လုပ်ထားတဲ့ file ဒါမှမဟုတ် အသစ် create လုပ်ထားတဲ့ file ကို git add <file> ဆိုပြီးတော့ add လုပ်ထားတဲ့ file တွေဖြစ်တယ်။

Git File Transitions



Git file transitions ဆိုတာကတော့ git က ဘယ် အခြေအနေမှာ file တွေကို ဘယ်မှာသိမ်းလဲ ဆိုတောတွေဖြစ်တယ်။ ယေဘုယျအားဖြင့်အောက်ပါအတိုင်း သတ်မှတ်နိုင်တယ်။

  • Working Directory
  • Staging Area
  • Local Repository
  • Remote Directory (Server)

Working Directory

Working Directory ကတော့ ကိုယ်နှစ်သက်ရာ နေရာကို server ကနေ git clone လုပ်ပြီးတော့ ယူထားတဲ့နေရာဖြစ်တယ်။ အခြေခံအားဖြင့်တော့ ကိုယ် အလုပ်လုပ်တဲ့နေရာဖြစ်တယ်။File/Folder တွေကို ဖျက်တာ၊ ပြင်တာ နဲ့ အသစ်ထည့်တာ လုပ်နိုင်တယ်။ 

Staging Area

Staging Area ကတော့ file ကို အသစ် create လုပ်ထားတာ၊ ဒါမှမဟုတ် modified လုပ်ထားတာတွေကို git add လုပ်လိုက်တဲ့ အခါမှာ git က file ကို  staged status နဲ့ သိမ်ထားတဲ့ နေရာကို staging area လိုခေါ်တယ်။ 

Local Repository

Local Repository ကတော့ staged ဖြစ်ပြီးသား file တွေကို git commit ဆိုတဲ့ command နဲ့ commit လုပ်လိုက်ရင် git က commit id တစ်ခုနဲ့အတူ Local Repository ပေါ်ကို သိမ်းတဲ့နေရာဖြစ်တယ်။ ဒီအခြေအနေမှာ file က remote repository (server) ကို မရောက်သေးဘူး။

Remote Repository (Server)

Remote Repository ကတော့ အပေါ်က Local Repository ကို commit လုပ်ပြီးသား တွေအားလုံးကို git push လုပ်လိုက်တဲ့ အခါမှာ Remote Repository ပေါ်ကို တင်လိုက်တယ်။ ဒီအချိန်မျာတော့ တင်လိုက်တဲ့ file တွေက Local နဲ့ Remote Repository ပေါ်ကနဲ့ အတူတူဖြစ်သွားပါပြီ။ Server ပေါ်တင်ပြီးသွားပြီဆိုတဲ့ အနေအထားဆိုတော့ စိတ်ချရတဲ့ အနေအထားပေါ့။



Git အခြေခံ Command များနှင့် အသုံးပြုနည်း

ပုံမှန် developer/user တစ်ယောက် အနေနဲ့ လိုအပ်တဲ့ git command တွေက တော့ အများအစား မရှိပါဘူး။ အောက်ပါ command တွေကို သေချာနားလည်ပြီး၊ အသုံးချတယ်မယ်ဆိုရင်တော့၊ အခြေခံအားဖြင့်တော့ git ကို ကောင်းကောင်း အသုံပြုနိုင်ပြီလို့ဆိုနိုင်ပါတယ်။ အခြေခံ command တွေကတော့ အောက်ပါအတိုင်းဖြစ်ပါတယ်။

  • git clone
  • git pull
  • git status
  • git add
  • git commit
  • git push
  • git checkout
  • git merge
  • git diff
  • git log
  • git stash
  • git cherry-pick


git clone

git clone ကတော့ ပထမဦးဆုံး local မှာ ဘာမှ မရှိသေးခင်မှာ သက်ဆိုင်ရာ Remote Repository ကနေ clone ယူတာဖြစ်ပါတယ်။
git clone https://github.com/kyawkyawlwin052/pigeon-myanmar-test.git


git pull

git pull ကတော့ Remote Repository (Server) ကနေပြီးတော့ latest changes တွေကို ယူဖို့သုံးတယ်။ Team နဲ့ အလုပ်လုပ်တဲ့ခါ server မှာ changes တွေက အမြဲတမ်းရှိနေတတ်တော့ pull လုပ်ဖို့ မမေ့ဖို့တော့လိုတယ်။
git pull


git status

Git status ကတော့ လက်ရှိ status ကိုကြည့်တာပါ။ လက်ရှိ ဘယ် branch မှာရောက်နေလဲ၊ untracked file တွေကဘာတွေလဲ၊ modified file တွေက ဘာတွေလဲ၊ commit လုပ်ထားတာတွေကဘာတွေလဲဆိုတာကို ကြည့်တာပါ။

git status


git add

git add ကတော့ file တစ်ခုကို အသစ် create လုပ်ထားတာ ဒါမှမဟုတ်ရင် modify လုပ်ထားတဲ့အခါမှာ git add ကို အသုံးပြုပြီး file တွေကို staged လုပ်တဲ့အခါ အသုံးပြုပါတယ်။ 

git add <file>

git add . (dot(.)ကတော့ လက်ရှိ directory အောက်က add မလုပ်ရသေးတာတွေ ကို add လုပ်ပေးပါတယ်)


git commit

git commit ကတော့ git add လုပ်ထားတဲ့ file တွေကို staged အဆင်ကတော့ Local Repository ကို commit လုပ်ချင်တဲ့ အခါမှာ သုံးပါတယ်။ git add မလုပ်ထားရင် commit လုပ်လို့မရပါဘူး။

git commit -m "Your message/memo"


git push

git push ကတော့ git commit လုပ်ထားတဲ့ file တွေ၊ တစ်နည်းအားဖြင့် Local Repository မှာရှိနေတဲ့ commit တွေကို Remote Repository (Server) ပေါ်တင်ချင်တဲ့ အခါမှာ သုံးရပါတယ်။ commit မလုပ်ရေသးတဲ့  file တွေကို push လုပ်လို့မရပါဘူး။ git add, git commit  အဆင့်ဆင့် လုပ်ပြီးသား တွေကိုပဲ​ push လုပ်လို့ရပါတယ်။ Local Repository ကနေ Remote Repository ကို တင်တဲ့ခါမှာ git push ကိုသုံးတယ်။

git push


git checkout

git မှာ developer တွေက branch တွေအများကြီးခွဲပြီး အလုပ်လုပ်လေ့ရှိကြပါတယ်။ ဥပမာ - ကိုယ်က User Registration function တစ်ခုကို စရေးတော့မယ်ဆိုရင်၊ feature/userRegistration ဆိုတဲ့ branch အတစ်ခုဆောက်ပြီးရေးတယ်။ ဘာလို့လဲဆိုတော ့Review လုပ်တဲ့ အချိန် compare လုပ်ရလွယ်ကူအောင်၊ ပြီးတော့ ဒီ feature ကို develop လုပ်နေတုန်းတော့ feature တစ်ခုမှာ error တက်နေရင် အရင်ရေးပြီးသားကုဒ်တွေမှာပါ run မရတော့တာတွေ မဖြစ်အောင် နဲ့ release လုပ်မယ် targed ထဲ မပါသေးရင် merge မလုပ်ပဲ ချန်ထားနိုင်အောင်ဖြစ်ပါတယ်။ git checkout ကိုတော့ အခြေခံအားဖြင့် branch တစ်ခုနဲ့ တစ်ခု ပြောင်းချင်တဲ့ အချိန်နဲ့ branch အတစ်တစ်ခု create လုပ်ချင်တဲ့ အချိန်မှာ သုံးပါတယ်။

ဒါကတော့ main branch ကနေပြီးတော့  feature/userRegistration ဆိုတဲ့  branch အသစ်တစ်ခုကို  ဆောက်တာဖြစ်ပါတယ်။ -b ဆိုတာက branch အသစ် create လုပ်တာကို ဆိုချင်တာဖြစ်ပါတယ်။ ဒီနေရာမှာ တစ်ခုပြောချင်တာက branch အသစ် ဆောက်ထားရင်လဲ branch က Local Repository မှာပဲရှိနေမှာပါ။ Remote repository ကို တင်ချင်တယ်ဆိုရင်တော့ ထုံးစံအတိုင်း push လုပ်ရပါမယ်။ push လုပ်ရတဲ့ command ကိုလဲ အောက်မှာ တစ်ခါတည်း ရေးပြထားပါတယ်။ -u ဆိုတာကတော့ upstream ကိုပြောချင်တာပါ local က feature/userRegistration နဲ့ remote က feature/userRegistration နဲ့ကို ချိန်ထားမယ်ပြောတာပါ။ 

git checkout -b feature/userRegistration main

git push -u origin feature/userRegistration


ဒါကတော့ branch တစ်ခုနဲ့ တစ်ခု ပြောင်းချင်တဲ့ခါသုံးပါတယ်။ ဥပမာ - လက်ရှိ branch က feature/userRegistration ဆိုပါစို့၊ အဲ့ဒီကနေ main branch ကိုပြောင်းချင်တယ်ဆိုရင်တော့  အောက်ပါအတိုင်းဖြစ်ပါတယ်။ git checkout က local changes တွေကို revert ပြန်လုပ်ချင်တဲ့ အခါမှာလဲ​ သုံးလို့ရပါတယ်။

git checkout main

git checkout <file>


git merge

git merge ကတော့ branch တစ်ခုကနေတစ်ခုကို merge လုပ်ဖို့ အသုံးပြုတာဖြစ်ပါတယ်။ ဥပမာ - feature/userRegistration branch က changes တွေကို main branch ထဲကို merge လုပ်ချင်တယ်ဆိုပါစို့။ အဲ့ဒါဆိုရင် လက်ရှိ branch က targed branch ဖြစ်ရပါမယ်။  git status နဲ့ current branch ကဘာလဲဆိုတာစစ်မယ်၊ main branch မဟုတ်သေးရင်၊ git checkout main ဆိုပြီးတော့ ပြောင်းမယ်။ ပြီးတာနဲ့ အောက်ပါ command ကို run ရပါမယ်။

git merge --no-ff feature/userRegistration

* --no-ff ကတော့ (no fast-forward, merge လုပ်တာကို commit တစ်ခုအနေနဲ့ လုပ်ချင်လို့ပါ - best practice)


git diff

git diff ကတော့ ရိုးရှင်းပါတယ် file တွေမှာ ဘာတွေပြောင်းထားလဲဆိုတာကို server နဲ့ compare လုပ်ပြီးပြပေးတာပါ။ git diff ကိုရိုးရိုး run ရင်တော့ ပြင်ထားသမျှ အားလုံးကို ပြပေးပြီးတော့၊ git diff <file> ဆိုရင်တော့ သက်ဆိုင်ရာ file မှာပဲ ပြင်ထားတာတွေကို ပြေပေးပါတယ်။

git diff

git diff <file>


git log

ကတော့ မှာ commit လုပ်ထားတဲ့ log တွေကို ပြပေးတယ်။ ဥပမာ - ဘယ်သူက၊ ဘယ်နေ့က ဘယ်အချိန် မှာ ဘာတွေ commit လုပ်ခဲ့တယ်ဆိုတာတွေ ပါ။ git log ကိုရိုးရိုး run ရင်တော့ ပြင်ထားသမျှ အားလုံးကို ပြပေးပြီးတော၊ git log -5 ဆိုရင်တော့ နောက်ဆုံး commit log ငါးခုကိုပြပေးမှာပါ။ 5 မှမဟုတ်ပါဘူး 1,2,3,... ကိုယ်ပြချင်သလောက်ကို သတ်မှတ်လို့ရပါတယ်။

git log

git log -3


git stash

Git stash ကတော့ developer တွေအတွက် အတော် အသုံးဝင်တဲ့ command ဖြစ်ပါတယ်။ git stash ကတော့ ခဏ သိမ်းထာပေးတယ်ဆိုတဲ့သဘောပါ။ ဘယ်လိုသုံးရလဲဆိုရင်တော့ ကိုယ်က local မှာ changes တွေကလည်း ရှိနေပြီ၊ commit လုပ်ဖို့ကလဲ ပြင်ထားတာတွေကသိပ်မသေချာသေးတော့ မလုပ်ချင်သေးဘူး၊ တစ်ခြား branch ဒါမှမဟုတ် feature တစ်ခုမှာလဲ အရေးပေါ် ပြင်ပေးစရာရှိနေတယ်ဆိုတဲ့ အခြေအနေမျိုးမှာ အသုံးဝင်ပါတယ်။ ဒါဆိုဘယ်လိုသုံးကြမလဲဆိုတော့ ကိုယ်လက်ရှိရှိပြီးသား changes တွေရှိနေတဲ့ အချိန်မှာ git stash လို့ run တာနဲ့ ရှိပြီးသား changes တွေကို temporary အနေနဲ့ သိမ်းပြီးတော့၊ working directory မှာတော့ ဘာမှမပြောင်းထားသလိုဖြစ်သွားပါတယ်။

git stash (local change တွေကို default message နဲ့သိမ်းပေးမယ်)

git stash save "Your message or memo"  (local change တွေကို ကိုယ်နှစ်သက်ရာ message)

git stash list (stash လုပ်ထားတာတွေအားလုံးကို list နဲ့ ကြည့်လို့ရတယ်)

git stash pop (ဒါကတော့ နောက်ဆုံး stash လုပ်ထားတာကို ကိုယ့် working directory ထဲပြန်ထည့်ပေးတယ်၊ ပြီးတော့ stash list ထဲကနေတစ်ခါတည်း ဖျက်ပေးတယ်)

git stash pop stash@{2} (ဒါကတော့ stash list လုပ်ပြီး ကိုယ်apply လုပ်ချင်တဲ့ stash index ကိုရွေးပြီး apply လုပ်တဲ့အခါသုံးတယ်၊ ပြီးတော့ stash list ထဲကနေတစ်ခါတည်း ဖျက်ပေးတယ်)

git stash apply (ဒါကတော့ pop လိုပဲ နောက်ဆုံး stash လုပ်ထားတာကို ကိုယ့် working directory ထဲပြန်ထည့်ပေးတယ်၊  stash list ထဲကနေမဖျက်ပေးဘူး)

git stash apply @stash{3} (ဒါကတော့ stash list လုပ်ပြီး ကိုယ်apply လုပ်ချင်တဲ့ stash index ကိုရွေးပြီး apply လုပ်တဲ့အခါသုံးတယ်၊ stash list ထဲကနေမဖျက်ပေးဘူး)


git cherry-pick

git cherry-pick တော့ အသုံးတော့သိပ်များဘူး၊ ဒါပေမဲ့ သိထားသင့်တယ်ထင်လို့ထည့်ပေးထားတာပါ။ cherry-pcik ကဘယ်လိုအလုပ်လုပ်လဲဆို ကိုလိုချင်တဲ့ commit id လေးကိုပဲ တခြား branch တစ်ခုကနေယူတဲ့အခါမှာသုံးရပါတယ်။ ကိုယ် လိုချင်တဲ့ commit id/hash ရဖို့အတွက် သက်ဆိုင်ရာ branch ကိုသွားပြီးတော့ git log မှာ commit id ကိုယူပါ။ ပြီးရင် ကို ရချင်တဲ့ target branch ကို git checkout နဲ့ပြောင်း၊ ပြီးရင်တော့ git cherry-pick <commit-hash> ကိုrun ပါ ပြီးရင်တော့ push လုပ်ပေါ့။

git cherry-pick <commit-hash>


Please see the English version here.


Git Basic Usage Demo


* ဒါကို သေချာနားမလည်သေးဘူးဆိုရင်တော့ အရင် post တစ်ခုဖြစ်တဲ့ Version Control ဖတ်ဖို့ အကြံပြုချင်ပါတယ်။

* Git repository ( မရှိထားရင် https://github.com/new account လုပ်ပြီး repo တစ်ခု create လုပ်ပါ)

* Git command တွေ run ဖို့  git bash တော့ ရှိဖို့လိုပါမယ်။  

https://git-scm.com/downloads

https://gitforwindows.org/


Related Articles:

Popular posts from this blog

Pigeon Myanmar's free Wireguard VPN

Pigeon Myanmar's free Wireguard VPN  Pigeon Myanmar ရဲ့ Free VPN service ကိုအသုံးပြုရန်အတွက် အောက်ပါ Telegram Chatbot ကို message ပို့ပြီး Config file ဒါမှမဟုတ် QR Code ကိုရယူနိုင်ပါတယ်။ အသုံးမပြုလိုသူများ၊ newvpn request မလုပ်ဖို့မတ္တာရပ်ခံအပ်ပါတယ်၊ client က limit ရှိတာကြောင့် လိုအပ်တဲ့သူတွေမရမှာစိုးလို့ပါ။ Telegram Chat Bot Telegram Chat Bot:  https://t.me/pigeonmyanmarbot Chat Bot မှာ /start ကိုလို့ပို့ပြီး သက်ဆိုင်ရာ Operation ကိုရေးပြီး VPN အသုံးပြုရန်အတွက် QR Code ဒါမှမဟုတ် Config file ရယူနိုင်ပါတယ်။ Telegram Account တစ်ခုကို client တစ်ခုသတ်မှတ်ထားဒါကြောင့် နောက်အကြိမ်တွေ ထပ်မှတ်ရယူပါကလည်း client အဟောင်းကိုသာ ရရှိတော့မှဖြစ်ပါတယ်။ QR Code/Config File ကို ပျောက်ပျက်သွားတဲ့အခါမှာလည်း ထပ်မံတောင်းခံ ကြည့်ရှူနိုင်ပါတယ်။   /newvpnqr - VPN QR Code ရယူရန်။ /newvpn - VPN Config File ရယူရန်။ VPN Installation and Setup သက်ဆိုင်ရာ Application ကိုအောက်ပါ link များမှ install လုပ်ပါ။  Application မှာ  + / Add a tunnel ခလုတ်နှိပ်ပါ။  Create from file or archive ဒါမှမဟုတ် Create from QR

Editor Config

Editor Config ကျွန်တော်တို့ Developer တွေတော်တော်များများက၊ ကိုယ်နဲ့ကျွမ်းကျင်ရာ ဒါမှမဟုတ် နှစ်သက်ရာ IDE/Text Editor တွေကို သုံးလေ့ရှိကြပါတယ်။ အဲ့နေရာမှာ ပြသနာတစ်ခုက IDE/Text Editor မတူတာနဲ့ Coding Style, Format တွေမှာ မတူတာ တွေဖြစ်တတ်ကြပါတယ်။ ဥပမာ - Eclipse သုံးတဲ့သူ အချင်းချင်းဆို setting ကို export လုပ်ပြီး အတူတူထားလို့ရပေမယ့်၊​ IntelliJ IDEA သုံးတဲ့သူတွေနဲ့ ဆို အဆင်မပြေဖြစ်ပြန်ရော။ Visual Studio Code, Atom, Sublime Text တွေကအများကြီးကို ကြားထဲ Indentation တွေ၊ Code Style တွေကို Standardize လုပ်ဖို့ ခက်ခဲလာတယ်။ အဲ့ဒါကြောင့် တစ်ချို့  Company/Project တွေဆိုရင် ဘယ် IDE/ Text Editor ပဲသုံးရမယ်ဆိုပြီးသတ်မှတ်ထားလေ့ရှိကြတယ်။ အကျိုးဆက်အနေနဲ့ကတော့ Productivity နဲ့ Developers တွေက သတ်မှတ်ထားတဲ့ tools တွေနဲ့ အကျွမ်းဝင်ဖို့ အချိန်ပေးရပါတယ်။ အဲ့ဒါ တွေကို Editor Config ကအလွယ်တကူဖြေရှင်းပေးနိုင်ပါတယ်။ Editor Config ရဲ့ အားသာချက်ကတော့ အတော်များများ IDE/ Text Editor တွေမှာ built-in ပါပြီးသားဖြစ်ပြီး၊ built-in မပါရင်လဲ Plugin တွေက အတော်များများအတွက်ရှိပါတယ်။  # EditorConfig is awesome: https://Edito