Skip to content

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 later

git stash 保存你的 uncommitted 更改 temporarily, 允许分支切换. Restore 它们使用 git stash pop 之后切换.

如何诊断

  • 运行 git 状态 到 see 哪个 files 是修改

  • Decide whether 更改应该是 kept or discarded

  • 检查如果更改是 staged or unstaged

如何修复

  • 提交更改使用 git 添加 and git 提交

  • Stash 它们 temporarily 使用 git stash and restore later 使用 git stash pop

  • Discard 更改使用 git checkout -- <file> 如果你 don't 需要它们

如何预防

  • 提交 or stash 之前切换分支 or pulling

  • 运行 git 状态 regularly 到 stay aware 的 uncommitted 更改

相关资源

相关术语

相关课程

相关练习

← 返回语言