กู้คืองานด้วย Reflog และ Stash
เชี่ยวชาญกลไกนิรภัยของ Git: ใช้ reflog เพื่อกู้คืนจากการรีเซ็ตและรีเบสที่ผิดพลาด และใช้ stash เพื่อพักและนำงานที่กำลังทำกลับคืนอย่างปลอดภัยระหว่างการกู้คืน
กู้คืองานด้วย Reflog และ Stash เป็นบทเรียน Git Advanced: Monorepo, Submodules & Workflows ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Git Advanced: Monorepo, Submodules & Workflows และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Git Advanced: Monorepo, Submodules & Workflows มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Git Rarely Loses Data
It feels like a bad reset or rebase destroys work, but Git almost never truly loses commits. They become unreachable, not deleted, and remain recoverable for weeks.
The reflog is your map back to them.
What the Reflog Records
The reflog logs every move of HEAD and branch tips: commits, checkouts, resets, rebases, merges. It is local and not shared, a private diary of where your refs have been.
git reflog
# a1b2c3d HEAD@{0}: reset: moving to HEAD~3
# 9f8e7d6 HEAD@{1}: commit: add featureRecovering After a Bad Reset
Ran git reset --hard and lost commits? Find the pre-reset SHA in the reflog and reset back to it.
git reflog
git reset --hard HEAD@{1}Recovering After a Bad Rebase
A rebase rewrites commits. If it went wrong, the reflog still holds the pre-rebase tip. Point your branch back at it.
git reflog
git reset --hard HEAD@{5} # the entry just before 'rebase'Restoring a Single Lost Commit
You do not always want to reset the whole branch. To resurrect just one commit, cherry-pick its SHA from the reflog.
git cherry-pick 9f8e7d6The Stash as a Safety Net
Before risky recovery operations, save uncommitted work with git stash. It tucks changes away cleanly so you can experiment without losing them.
git stash push -m 'wip before recovery'Inspecting and Restoring Stashes
List stashes and bring one back. apply keeps it in the list; pop restores and removes it.
git stash list
git stash show -p stash@{0}
git stash pop stash@{0}Recovering Dropped Stashes
Dropped a stash by mistake? Stashes are commits too. Find the dangling commit and re-apply it.
git fsck --no-reflogs | grep commit
git stash apply <dangling-sha>Finding Orphans with fsck
When even the reflog does not show it, git fsck --lost-found hunts for dangling commits and blobs, the last-resort recovery tool.
git fsck --lost-foundThe Garbage Collection Clock
Unreachable objects survive until garbage collection prunes them, by default after about 30 days for reflog entries. Recover promptly; do not let weeks pass before you go looking.
The Reflog Is Local Only
Remember: the reflog lives only in your clone. A teammate's reflog will not show your moves, and a fresh clone has none. If you need shared recoverability, push branches or tags before risky operations.
Quick Check
Test your understanding of Git recovery tools.
Recap
You learned Git's safety nets: the reflog recovers from bad resets and rebases, cherry-pick restores single lost commits, the stash protects work-in-progress, fsck hunts dangling objects, and the GC clock means you should recover promptly. Git rarely loses data if you know where to look.
คำถามที่พบบ่อย
บทเรียน “กู้คืองานด้วย Reflog และ Stash” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “กู้คืองานด้วย Reflog และ Stash” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Git Advanced: Monorepo, Submodules & Workflows ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Git Advanced: Monorepo, Submodules & Workflows มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “กู้คืองานด้วย Reflog และ Stash”
เชี่ยวชาญกลไกนิรภัยของ Git: ใช้ reflog เพื่อกู้คืนจากการรีเซ็ตและรีเบสที่ผิดพลาด และใช้ stash เพื่อพักและนำงานที่กำลังทำกลับคืนอย่างปลอดภัยระหว่างการกู้คืน คุณปฏิบัติ Git Advanced: Monorepo, Submodules & Workflows ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Git Advanced: Monorepo, Submodules & Workflows หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Git Advanced: Monorepo, Submodules & Workflows บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “กู้คืองานด้วย Reflog และ Stash” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Git Advanced: Monorepo, Submodules & Workflows นี้ได้ไหม
ได้ บทเรียน Git Advanced: Monorepo, Submodules & Workflows ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การกู้คืนคอมมิตและแขนงที่สูญหาย
- การแก้ไขข้อบกพร่องด้วย Git Bisect
- การเพิ่มประสิทธิภาพคลัง Git
- กู้คืองานด้วย Reflog และ Stash