နားလည်ထားရမယ့် အချက်
Git ရဲ့ 'undo' အခြေအနေတိုင်းက မေးခွန်းတစ်ခုတည်းအတိုင်း ရောက်ရှိသွားပါတယ် — အမှားက အခု ဘယ်အခြေအနေမှာ ရှိနေလဲ။ ဒါကို ဖြေနိုင်ရင် ဘယ် tool သုံးရမလဲဆိုတာ အတိအကျ သိနိုင်ပါတယ်။
- Uncommitted ပြောင်းလဲမှု ဖျောက်ချင်တာ -> `git restore <file>`
- မတော်တဆ stage လုပ်မိတာ, commit မလုပ်ရသေး -> `git restore --staged <file>`
- Local-only commit, ခုနစ်လုပ်ခဲ့တာ -> `git commit --amend`
- Local-only commit, နောက်ကျတာ (သို့) များတာ -> `git reset`
- Push လုပ်ပြီး share ဖြစ်သွားတဲ့ commit -> `git revert`
`git commit --amend` က နောက်ဆုံး commit ကို history ထဲက နေရာတူမှာပဲ လုံးလုံးလျားလျား ပြန်ရေးပေးပြီး၊ `git reset` က local commit အများကြီးကို တစ်ခါတည်း ပြန်ချေဖျက်ရင် mode အလိုက် ပြောင်းလဲမှုတွေ staged ဖြစ်မလား working directory ထဲမှာ ကျန်မလား ဆုံးဖြတ်ပေးပါတယ်။
Commit တစ်ခု push လုပ်ပြီး တစ်ယောက်ယောက် pull ယူသွားနိုင်ပြီဆိုရင် `git revert` ကို အစားထိုးသုံးပါ — ဘာမှ ပြန်မရေးဘဲ commit အသစ်တစ်ခုနဲ့ ပြန်ဖျက်ပေးလို့ ဘယ်သူ့ clone မှ ပြန်ရေးမခံရပါဘူး။
WHAT STATE IS YOUR MISTAKE IN?
------------------------------
WHAT STATE IS YOUR MISTAKE IN?
---------------------------------
uncommitted, unstaged change -> git restore <file>
uncommitted, staged by mistake -> git restore --staged <file>
committed, local only (latest) -> git commit --amend
committed, local only (older) -> git reset
committed, already pushed/shared -> git revertလက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်
မှားယွင်း stage လုပ်မိတာ
`.env.local` ကို `git add` လုပ်မိရင် `git restore --staged .env.local` က untracked ဆီ ပြန်ရောက်စေပါတယ်၊ ဘာမှ မဆုံးရှုံးပါဘူး။
Commit ကို ပြင်ချင်တာ
Commit ဝင်ပြီးနောက် တစ်ခုခု လိုနေတာ တွေ့ရင် ပြင်ပြီး stage လုပ်ပြီး `git commit --amend` ကို run ပါ — Git က previous commit ကို နေရာတူမှာပဲ ပြန်ရေးပေးပါတယ်။
ဒီ lesson က ယခင် lesson တွေကို ပေါင်းစည်းပေးတာပါ
Restore, stash, revert, reset တို့ဟာ ယခင်က ဖော်ပြခဲ့သလိုပဲ အတိအကျ အလုပ်လုပ်ပါတယ် — ဒီ lesson က ဘယ် tool ကို ဘယ်အချိန် ရွေးရမလဲဆိုတဲ့ မြေပုံသက်သက်ပါ။
အတူတူ စမ်းရေးကြည့်မယ်
git init -q -b main
git config user.name "Thuta Learner"
git config user.email "learner@example.com"
export GIT_AUTHOR_NAME="Thuta Learner"
export GIT_AUTHOR_EMAIL="learner@example.com"
export GIT_COMMITTER_NAME="Thuta Learner"
export GIT_COMMITTER_EMAIL="learner@example.com"
echo "console.log('core');" > app.js
git add app.js
GIT_AUTHOR_DATE="2026-01-08T09:00:00" GIT_COMMITTER_DATE="2026-01-08T09:00:00" \
git commit -m "Initial commit"
git branch -M main
echo "SECRET_KEY=abc123" > .env.local
git add .env.local
git status
git restore --staged .env.local
git status
rm .env.local
echo "console.log('feature v1');" >> app.js
git add app.js
GIT_AUTHOR_DATE="2026-01-08T10:00:00" GIT_COMMITTER_DATE="2026-01-08T10:00:00" \
git commit -m "add feature"
git log --oneline
echo "console.log('feature v1 finished');" >> app.js
git add app.js
GIT_AUTHOR_DATE="2026-01-08T10:05:00" GIT_COMMITTER_DATE="2026-01-08T10:05:00" \
git commit --amend -m "Add feature v1"
git log --oneline
git show --stat HEAD$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: .env.local
$ git restore --staged .env.local
$ git status
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
.env.local
nothing added to commit but untracked files present (use "git add" to track)
$ git log --oneline
deb9ba8 add feature
85d10e3 Initial commit
$ git commit --amend -m "Add feature v1"
$ git log --oneline
06c60a7 Add feature v1
85d10e3 Initial commit
$ git show --stat HEAD
commit 06c60a7d3c9aac1b63ec91514d72939ce4d1825a
Author: Thuta Learner <learner@example.com>
Date: Thu Jan 8 10:00:00 2026 +0900
Add feature v1
app.js | 2 ++
1 file changed, 2 insertions(+)၅ မိနစ် စမ်းကြည့်
File တစ်ခုကို မှားယွင်းစွာ stage လုပ်ပြီး `git restore --staged` နဲ့ content ဆုံးရှုံးမသွားဘဲ ပြန်ဖျက်ပါ။ ပြီးရင် commit စစ်စစ်တစ်ခု လုပ်ပါ၊ တစ်ခုခု မေ့နေတာ သတိထားမိပါ၊ ပြင်ပြီး commit အသစ်မလုပ်ဘဲ `git commit --amend` နဲ့ ပြင်ဆင်ချက်ကို commit အတူတူထဲ ထည့်ပါ။ အဆင့်တစ်ခုစီအလိုက် အတိအကျ ဘာပြောင်းသွားလဲ `git log --oneline` နဲ့ နှိုင်းယှဉ်ကြည့်ပါ။
သတိလေးတစ်ချက်
`git commit --amend` က နောက်ဆုံး commit ရဲ့ hash ကို ပြောင်းပစ်ပါတယ် — ထို commit ကို push လုပ်ပြီးသားဆိုရင် amend လုပ်ပြီး ပြန် push ဖို့ force-push လိုအပ်ပြီး push လုပ်ပြီးသား commit ကို reset လုပ်တာနဲ့ risk အတူတူပါပဲ။
`git restore <file>` (`--staged` မပါဘဲ) က working directory ပြောင်းလဲမှုကို အမြဲတမ်း ဖျက်ပစ်ပါတယ် — run မလုပ်ခင် unstage ချင်ရုံလား ဘာမှ မလိုချင်တော့ဘူးလား သေချာအောင် စစ်ဆေးပါ။
Git Documentation: git-restore — Git & GitHub