Git 结帐不会还原文件

Git checkout doesnt revert files

在mac(山脉)上使用git; git 版本 2.8.4(苹果 Git-73)

git checkout <master>

命令不会还原在另一个分支上所做的文件更改。

这是我所做的。

cd project_folder
git clone -o origin -b master git@gitlab.corp.xyz.com:prj/PRODUCTION.git
git branch -b new_branch
git checkout new_branch
<make changes to an existing file>
git checkout master

我期待看到一个干净的 master 版本,没有 new_branch 中所做的更改。但是当我在分支之间切换时,我所做的更改会继续进行。

以前(很久以前)这曾经像我预期的那样工作。我错过了什么吗?

我进行了大量 google 和 Whosebug 搜索。没有产生我正在寻找的结果。我得到的最接近的结果是,但它们对我帮助不大。 1. https://superuser.com/questions/892847/git-branch-branches-not-different 2.Git branch switching does not change code folder files

这是预期的行为,也是我在使用 Git 时通常看到的情况。来自 documentation 的 git 结帐:

To prepare for working on <branch>, switch to it by updating the index and the files in the working tree, and by pointing HEAD at the branch. Local modifications to the files in the working tree are kept, so that they can be committed to the <branch>.

如果您需要切换分支并且您的工作目录不干净,这里有几个选项:

1) 您可以在当前分支上进行临时提交:

git commit -m 'WIP'

我说 "temporary" 但实际上这就像任何其他提交一样。唯一的区别是,当您 return 到此分支时,您将 修改 此提交,一旦您通过以下方式完成任务:

git commit --amend -m 'Finished WIP'

2) 通过

隐藏您的更改
git stash

存储将创建两个 提交,一个用于工作目录,一个用于阶段。当您想恢复这些更改时,您可以通过以下方式应用这些更改:

git stash apply