使用引用日志和暂存区挽救工作
掌握 Git 的安全机制:使用引用日志从错误的重置和变基中恢复,并使用暂存区安全地搁置和还原恢复过程中的未完成工作。
使用引用日志和暂存区挽救工作 是 CoddyKit 上的免费 Git Advanced: Monorepo, Submodules & Workflows 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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.
常见问题解答
「使用引用日志和暂存区挽救工作」课时是免费的吗?
是的 — 「使用引用日志和暂存区挽救工作」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Git Advanced: Monorepo, Submodules & Workflows 课程的其余内容,请升级到 CoddyKit PRO。 Git Advanced: Monorepo, Submodules & Workflows 课程共包含 4 节课。
「使用引用日志和暂存区挽救工作」这节课中我会学到什么?
掌握 Git 的安全机制:使用引用日志从错误的重置和变基中恢复,并使用暂存区安全地搁置和还原恢复过程中的未完成工作。 你通过在浏览器中直接运行的动手代码来练习 Git Advanced: Monorepo, Submodules & Workflows,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Git Advanced: Monorepo, Submodules & Workflows 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Git Advanced: Monorepo, Submodules & Workflows 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用引用日志和暂存区挽救工作」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Git Advanced: Monorepo, Submodules & Workflows 课中编写并运行代码吗?
能。每节 Git Advanced: Monorepo, Submodules & Workflows 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 恢复丢失的提交与分支
- 使用 Git Bisect 调试
- 优化 Git 代码仓库性能
- 使用引用日志和暂存区挽救工作