Uncommitted 更改
错误信息
错误: 你的局部更改到 following files 会是 overwritten
快速摘要
Git 停止你因为切换分支 or pulling 会 overwrite 更改你 haven't committed yet.
为什么发生
你有修改 files 那个 haven't 是 committed
你 tried 到切换分支使用 dirty 工作树
拉取 or checkout 会 overwrite 你的局部更改
最小示例
✗ 错误代码
git checkout main # error: local changes would be overwritten✓ 修复代码
git stash
git checkout main # succeeds
git stash pop # restore changes latergit stash 保存你的 uncommitted 更改 temporarily, 允许分支切换. Restore 它们使用 git stash pop 之后切换.
如何诊断
运行
git 状态到 see 哪个 files 是修改Decide whether 更改应该是 kept or discarded
检查如果更改是 staged or unstaged
如何修复
提交更改使用
git 添加andgit 提交Stash 它们 temporarily 使用
git stashand restore later 使用git stash popDiscard 更改使用
git checkout -- <file>如果你 don't 需要它们
如何预防
提交 or stash 之前切换分支 or pulling
运行
git 状态regularly 到 stay aware 的 uncommitted 更改
相关资源
相关练习
- Git 状态 Command 测验
测试你的 knowledge 的 git 状态 for uncommitted 更改.
- Spot Git 提交 Mistake
练习 spotting Git 提交 mistakes.