Skip to content

局部更改会是 Overwritten

错误信息

                错误: 你的局部更改到 following files 会是 overwritten 通过 checkout
              

快速摘要

切换分支会 overwrite 你的 uncommitted 更改, 所以 Git 停止到 protect 你的工作.

为什么发生

  • 你有修改 files 那个 conflict 使用目标分支's version

  • 你 tried 到切换分支使用 dirty 工作树

  • 拉取 or checkout 会 overwrite 局部 modifications

最小示例

✗ 错误代码
git checkout main  # error: local changes would be overwritten
✓ 修复代码
git stash
git checkout main  # succeeds
git stash pop  # restore changes

Stash 保存你的更改 temporarily, 允许分支切换. Pop restores 它们 afterward.

如何诊断

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

  • Decide 如果更改应该是 kept, committed, or discarded

  • 检查如果更改是 staged or unstaged

如何修复

  • 提交更改: git 添加 . && git 提交 -m "Save work"

  • Stash 它们: git stash, 然后 checkout, 然后 git stash pop later

  • Discard 它们: git checkout -- <file> (如果你做不需要更改)

  • Force checkout: git checkout -f <分支> (destroys 局部更改)

如何预防

  • 提交 or stash 之前切换分支

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

相关资源

相关术语

相关课程

← 返回语言