မဂၤလာပါ။
ကြၽန္ေတာ္ကေတာ့ Spiceworks Myanmar မွာ Backend Developer အေနနဲ႔ တာဝန္ယူလုပ္ကိုင္ေနတဲ့ သုတယာမိုး ျဖစ္ပါတယ္။ ဒီတစ္ေခါက္မွာေတာ့ အသုံးမ်ားတဲ့ git commands အေၾကာင္းေတြကို ကြၽန္ေတာ္သိသေလာက္ ေဝမွ်ေပးခ်င္ပါတယ္။
Git ကေတာ့ team members, developers ေတြ စုေပါင္းၿပီး project ေတြကို ျမန္ျမန္ဆန္ဆန္၊ ထိထိေရာက္ေရာက္ လုပ္ႏိုင္ေစတဲ့ verison control system (VCS) တစ္ခုျဖစ္ပါတယ္။ VCS ျဖစ္တဲ့အတြက္ code ေတြ ဘယ္ file၊ ဘယ္ folder ေတြမွာ ေျပာင္းလဲထားတယ္ဆိုတာကို မွတ္ထားေပးတယ္၊ အရင္ေရးၿပီးသား version က ကုဒ္နဲ႔ အခုေရးထားတဲ့ကုဒ္နဲ႔ ကြာသြားတာေတြကို ျပသေပးတယ္၊ ကိုယ္လိုခ်င္တဲ့ version ကိုလည္း ျပန္ၿပီး restore လုပ္ႏိုင္ပါတယ္။
Working, Staging & Repository Areas

Git မွာ အဓိက်တဲ့ areas သုံးမ်ိဳးရွိပါတယ္။
1. Working Directory
Woring Directory မွာေတာ့ ကြၽန္ေတာ္တို႔ file နဲ႔ folder အသစ္ေတြထပ္တိုးမယ္၊ code ေတြကို ေျပာင္းလဲမႈေတြလုပ္တဲ့ေနရာျဖစ္ပါတယ္။
2. Staging Area
Staging Area မွာေတာ့ local repository ထဲကို မသြားခင္ ေနရာျဖစ္ပါတယ္။ ဒီေနရာမွာ working directory ထဲကေန ေနာက္ commit မွာပါေစခ်င္တဲ့ ေျပာင္းလဲမႈေတြကို သိမ္းထားေပးပါတယ္။ git add လုပ္ရင္ ေရာက္ရွိမယ့္ေနရာလို႔လည္း ေျပာလို႔ရပါတယ္။
3. Repository Area
Repository Area ကေတာ့ git add လုပ္ၿပီးေနာက္ staging area အျဖစ္နဲ႔ရွိေနတဲ့အရာအားလုံးကို git commit လုပ္တဲ့အခ်ိန္မွာ local repository (.git/ folder) ထဲကိုပို႔ေပးမွာျဖစ္ပါတယ္။
Git Init
Project တစ္ခုမွာ git သုံးမယ္ဆိုရင္ git init (သို႔) git clone နဲ႔ အစျပဳလို႔ရပါတယ္။ git init ကို project က local machine ထဲမွာပဲ ရွိေသးတဲ့ အေျခအေနမ်ိဳးမွာ အသုံးမ်ားပါတယ္။ git clone ကေတာ့ github, gitlab အစရွိတာေတြမွာ remote repository ရွိၿပီးတဲ့ အေျခအေနမ်ိဳးမွာ သုံးပါတယ္။ git init လုပ္ၿပီးၿပီဆိုရင္ေတာ့ ကိုယ္ရဲ႕ project directory ေအာက္မွာ .git/ folderကို ရရွိလာမွာျဖစ္ပါတယ္။
cd /project-folder git init
Git Repository
Git respository ကေတာ့ ကိုယ္ေရးေနတဲ့ project ရဲ႕ ေျပာင္းလဲမႈအားလုံးကို သိမ္းထားေပးတဲ့ေနရာျဖစ္တဲ့ .git/ folder ကိုပဲဆိုလိုတာျဖစ္ပါတယ္။ Local repository ကေတာ့ local machine ထဲက repository ကိုေခၚတာျဖစ္ၿပီး remote repository ကေတာ့ server တစ္ခုမွာ တင္ထားတဲ့ repository ကိုေခၚတာျဖစ္ပါတယ္။
Git Clone
git clone ကိုေတာ့ github ေပၚမွာရွိတဲ့ မိမိရဲ႕ remote git repository (သို႔) သူမ်ားရဲ႕ repository ကို ကိုယ့္ရဲ႕ local machine ထဲ copy ကူးလိုတဲ့အခါ၊ ကိုယ္ရဲ႕ local repo ကိုပဲ ေနရာေျပာင္းခ်င္တဲ့အခါ အသုံးျပဳပါတယ္။
git clone git@github.com:username/projectname.git git clone git@github.com:username/projectname.git mygit git clone git@github.com:username/projectname.git .
Line 1 command ကို သုံးမယ္ဆိုရင္ /projectfolder/projectname ဆိုၿပီး directory တစ္ခု ေဆာက္ေပးမွာျဖစ္ၿပီး သူ႔ထဲကိုမွ remote repo က files မ်ား ဝင္သြားမွာျဖစ္ပါတယ္။
Line 2 command ကို သုံးမယ္ဆိုရင္ /projectfolder/mygit ဆိုၿပီး directory တစ္ခု ေဆာက္ေပးမွာျဖစ္ၿပီး သူ႔ထဲကိုမွ remote repo က files မ်ား ဝင္သြားမွာျဖစ္ပါတယ္။
Line 3 command ကို သုံးမယ္ဆိုရင္ /projectfolder/ folder ထဲကိုပဲ remote repo က files မ်ား ဝင္သြားမွာျဖစ္ပါတယ္။
Git Add
git add ကေတာ့ working area ထဲကေန staging area ကို ပို႔ေပးတဲ့ command ျဖစ္ပါတယ္။
git add . git add <filename>
git add . ဆိုရင္ေျပာင္းလဲမႈရွိတဲ့ file မွန္သမွ်ကို staging area ကို ပို႔ေပးမွာျဖစ္ပါတယ္။
ကိုယ္လိုခ်င္တဲ့ file ကိုပဲ staging area ထဲကို ပို႔ေစခ်င္တဲ့အခါ git add “filename” command သုံးရပါမယ္။
Git Commit
git commit ကေတာ့ staging area ကေန repository area (.git/) ကို ပို႔ေပးတာျဖစ္ပါတယ္။ git commit လုပ္တဲ့အခါ commit ရဲ႕ message ေတြထည့္ေပးရပါတယ္။ ထိုအခါမွသာ တျခားသူက ဒီ commit မွာ ဘာေတြပါဝင္လဲဆိုတာကို နားလည္လြယ္မွာ ျဖစ္ပါတယ္။
git commit -m "message" git commit -m "message" -m "another message"
Git Reset
git reset ကေတာ့ git add နဲ႔ ဆန္႔က်င္ဘက္ျဖစ္ၿပီး staging area ကေန working directory area ကို ျပန္ပို႔ေပးပါတယ္။ git reset ကို commits ေတြ ဖ်က္လိုတဲ့အခါမွာလည္း အသုံးျပဳပါတယ္။
git reset . git reset <filename> git reset --soft <commit_id> git reset --hard <commit_id>
–soft ကေတာ့ အခု reset လုပ္မယ့္ commit ကို repository area ထဲ မထည့္ခင္အခ်ိန္က ကိုယ္ changes လုပ္ထားတာေတြ က်န္ရွိေနေစခ်င္တဲ့ အခါသုံးပါတယ္။
–hard ကေတာ့ changes ေတြကိုပါ ဖ်က္ေပးသြားေစခ်င္တဲ့အခါ သုံးပါတယ္။
Git Fetch
git fetch ကေတာ့ remote repository မွာ ဘာေတြေျပာင္းလဲေနတယ္၊ commit ဘယ္ႏွစ္ခုက local repository ထက္ ေရွ႕ဘယ္ႏွစ္ခုေလာက္ေရာက္ေနတယ္ ဆိုတာေတြကို checking လုပ္တဲ့ command ျဖစ္ပါတယ္။
git fetch <remotename>
Git Pull
git pull ကေတာ့ local repository ကို remote repository အတိုင္း up to date ျဖစ္ေစဖို႔၊ အျခားကိုယ္နဲ႔တူတူ တြဲလုပ္ေနတဲ့သူေတြ တင္ထားတာေတြ ကိုယ့္ရဲ႕ local repository ထဲ ေရာက္လာေစဖို႔ သုံးပါတယ္။
git pull <remotename> <branchname>
Git Push
git push ကေတာ့ ကိုယ့္ local repository ထဲက ေျပာင္းလဲမႈေတြကို တျခား ကိုယ္နဲ႔အတူတူ တြဲလုပ္ေနတဲ့သူေတြ သိေစဖို႔ သုံးပါတယ္။
git push -u <remotename> <branchname>
Git Status
git status ကေတာ့ working directory နဲ႔ staging area ထဲက ေျပာင္းလဲမႈေတြကို ျပေပးပါတယ္။ ဘယ္ branch မွာအခု ေရာက္ေနလဲ၊ ဘယ္ file ေတြက ေျပာင္းလဲမႈေတြလုပ္ထားလဲ၊ ဘယ္ file ေတြက အသစ္ထည့္ထားလဲ အစရွိတာေတြကိုျပေပးတဲ့ command ျဖစ္ပါတယ္။
git status
Git Diff
git diff ကေတာ့ ကြာျခားမႈေတြကိုျပေပးတဲ့ command ျဖစ္ပါတယ္။
git diff git diff index.html git diff --staged git diff --staged index.html git diff <commit>..<commit>
Line 1 command ကေတာ့ working directory နဲ႔ last commit ႏွစ္ခုၾကားက ကြာျခားမႈေတြကိုျပေပးမွာျဖစ္ပါတယ္။
Line 2 command ကေတာ့ working directory နဲ႔ last commit ႏွစ္ခုၾကားက ကိုယ္လိုခ်င္တဲ့ file ရဲ႕ ကြာျခားမႈေတြကိုျပေပးမွာျဖစ္ပါတယ္။
Line 3 command ကေတာ့ git add လုပ္ၿပီးသြားတဲ့ အေျခအေနမွာျဖစ္ပါတယ္။ သူကေတာ့ staging area နဲ႔ last commit ႏွစ္ခုၾကားက ကြာျခားမႈေတြကိုျပေပးမွာျဖစ္ပါတယ္။
Line 4 command ကေတာ့ staging area နဲ႔ last commit ႏွစ္ခုၾကားက ကိုယ္လိုခ်င္တဲ့ file ရဲ႕ ကြာျခားမႈေတြကိုျပေပးမွာျဖစ္ပါတယ္။
Line 5 command ကေတာ့ commits ႏွစ္ခုၾကားက ကြာျခားမႈေတြကိုျပေပးမွာျဖစ္ပါတယ္။
Git Branch
git branch ကေတာ့ git ကို စၿပီး initialize လုပ္ကတည္းက master branch ကေတာ့ default ပါလာမွာျဖစ္ပါတယ္။ Master branch က production လုပ္မယ့္ code ေတြပဲ ထားေနက်ေနရာျဖစ္တဲ့အတြက္ အေျပာင္းအလဲေတြလုပ္ဖို႔႔ ေကာင္းတဲ့ ေနရာမဟုတ္ပါဘူး။ တစ္ေယာက္တည္းဆိုရင္ ျပႆနာမဟုတ္ေပမယ့္ team နဲ႔ ေရးတဲ့အခါမ်ိဳးမွာ branch တစ္ခုပဲသုံးေနမယ္ဆိုရင္ code conflicts ေတြ ခဏခဏျဖစ္ေနမွာျဖစ္ပါတယ္။ အဲ့ဒါေၾကာင့္ သက္ဆိုင္ရာ branch ေတြခြဲၿပီး ေရးဖို႔ အတြက္ သုံးပါတယ္။
git branch -a git branch <branchname> git checkout -b <branchname> git checkout <branchname> git checkout -d <branchname>
Line 1 command ကေတာ့ repository ထဲက branch အားလုံးကို ေဖာ္ျပေပးမွာျဖစ္ပါတယ္။
Line 2 command နဲ႔ Line 3 command ကေတာ့ အသစ္ branch တစ္ခု ေဆာက္ေပးမွာ ျဖစ္ပါတယ္။
Line 4 command ကေတာ့ အခု ရွိၿပီးသား branch တစ္ခုဆီသို႔ ေျပာင္းေပးတဲ့ command ျဖစ္ပါတယ္။
Line 5 command ကေတာ့ branch ကို delete လုပ္မွာျဖစ္ပါတယ္။
Git Merge
git merge ကေတာ့ ကြဲေနတဲ့အရာႏွစ္ခုကို တစ္ခုတည္းျဖစ္ေအာင္ ျပန္ေပါင္းေစဖို႔ သုံးပါတယ္။ Master branch က new_feature branch က ေျပာင္းလဲမႈေတြကို လိုခ်င္တဲ့အခါ merge လုပ္ေပးျခင္းျဖင့္ new_feature branch က changes အားလုံးက main branch ဆီမွာ ေပါင္းေပးသြားမွာ ျဖစ္ပါတယ္။
git merge new_feature
Git Stash
git stash ကေတာ့ feature-a branch မွာ ကိုယ္အသစ္ေရးထားတဲ့ code ေတြကို repostitory area ထဲကို သိမ္းဖို႔ အဆင္သင့္ မျဖစ္ေသးဘဲ feature-b branch ကို သြားလိုတဲ့အခါမ်ိဳးမွာ code ေတြကို မဆုံးရႈံးေအာင္ သုံးပါတယ္။ stash changes လုပ္ထားၿပီးၿပီဆိုပါက လိုတဲ့အခ်ိန္ ျပန္ေခၚသုံးလို႔ရမွာျဖစ္ပါတယ္။
git stash
git stash save "message"
git stash list
git stash apply stash@{n}
git stash drop stash@{n}
git stash clear
Line 1 command နဲ႔ Line 2 command ကေတာ့ changes ေတြကို stash လုပ္ေပးမွာျဖစ္ပါတယ္။ “message” ကေတာ့ stash က ဘာအတြက္လုပ္တာလဲဆိုတာကို သိလြယ္ေအာင္ ထည့္ေပးႏိုင္ပါတယ္။
Line 3 command ကေတာ့ repository ထဲက stash list အားလုံးကို ေဖာ္ျပေပးမွာ ျဖစ္ပါတယ္။
Line 4 command ကေတာ့ stash list ကေန ကိုယ္လိုခ်င္တဲ့ stash number ကိုပဲ ျပန္ယူသုံးမွာျဖစ္ပါတယ္။
Line 5 command ကေတာ့ stash list ကေန ကိုယ္ဖ်က္ခ်င္တဲ့ stash number ကို ဖ်က္ေပးမွာျဖစ္ပါတယ္။
Line 6 command ကေတာ့ repository ထဲက stash list အားလုံးကို ဖ်က္ေပးမွာျဖစ္ပါတယ္။
Git Log
git log ကေတာ့ repository ရဲ႕ ျဖစ္ခဲ့သမွ် မွတ္တမ္းေတြကို ျပန္ၾကည့္ဖို႔ သုံးပါတယ္။
git log git log --oneline --decorate --graph
.gitignore
Git ကေန track မလုပ္ေစခ်င္တဲ့ file ေတြ၊ folder ေတြကို .gitignore file ထဲမွာ ေရးေပးရပါမယ္။
file.txt folder/ *.dmg *.apk
အခ်ိန္ေပးၿပီးဖတ္ေပးတဲ့အတြက္ ေက်းဇူးတင္ပါတယ္ခင္ဗ်။
