Git | “Your local changes to the following files would be overwritten by checkout”报错!!!

Git | “Your local changes to the following files would be overwritten by checkout”报错!!!
小陈好惨!!!赶项目赶了一天,结果~整天出错!

error: Your local changes to the following files would be overwritten by checkout:
        miniprogram/pages/index/index.js
Please commit your changes or stash them before you switch branches.
Aborting

提示:
当前分支有未跟踪的文件,checkout 命令会覆盖它们,请缓存( stash )或者提交( commit )

两种选择:

1、未保存文件很要~保存修改

//第一种方式 存到暂存区
git add.
git stash 
//取出的时候使用 
git stash pop

//第二种方式 发起一个commit 存到提交历史
git add.
git commit -m "commit message"

2、未跟踪文件的内容改动不重要,放弃修改!清除修改和强制切换分支

推荐做法:清除未跟踪文件

git clean n  //这个是清除文件预览
git clean -f //强制清除文件

Git | “Your local changes to the following files would be overwritten by checkout”报错!!!

强制切换分支

强制切换分支命令如下,结果同提示说的那样,会直接覆盖未跟踪的文件。这个方式我觉得很是粗暴,我们日常切换的时候,还是不要使用 -f 强制切换,没有覆盖提示,很容易发生文件修改丢失,但是我们自己不知道。

git checkout -f <branch> 

Git | “Your local changes to the following files would be overwritten by checkout”报错!!!

现在分析一下原理:Git本地版本控制分为有三个部分:工作区(Working Directory)、暂存区(Staged Snapshot)、版本库区 (Commit History)
Git | “Your local changes to the following files would be overwritten by checkout”报错!!!
执行 checkout 操作的时候,git 会检查工作区是否存在未跟踪文件