为什么局部变化似乎在两个分支中同样存在?

Why do local changes seem to exist equally in both branches?

我的 git 存储库中有不同的分支。我签出一个新分支并应用更改。当我结帐回我的 master 分支时,它仍然显示更改。同样,当我在新分支中暂存更改以提交,然后检出主分支时,它再次显示这些更改已暂存。

为什么即使我切换回我的分支 master,我的分支 regrid-more-flexible-times 中的更改似乎仍显示(暂存或未暂存)?见下图。

$ git branch
* master
  regrid-more-flexible-times
  regrid-sst
$ git checkout regrid-more-flexible-times
Switched to branch 'regrid-more-flexible-times'
$ git status
# On branch regrid-more-flexible-times
nothing to commit (working directory clean)
$ vim regridworkflow.py
$ git status
# On branch regrid-more-flexible-times
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   regridworkflow.py
#
no changes added to commit (use "git add" and/or "git commit -a")

到目前为止一切顺利。我在我的分支 regrid-more-flexible-times 中进行了更改。现在我需要返回 master 访问代码库而不做任何更改。

$ git checkout master
M       mms/src/main/python/regridworkflow.py
Switched to branch 'master'
$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   regridworkflow.py
#
no changes added to commit (use "git add" and/or "git commit -a")

嗯?为什么分支 master 显示我在分支 regrid-more-flexible-times 上所做的未暂存更改?可能是我上演之后才知道有区别吧?

$ git checkout regrid-more-flexible-times
M       mms/src/main/python/regridworkflow.py
Switched to branch 'regrid-more-flexible-times'
$ git add regridworkflow.py
$ git status
# On branch regrid-more-flexible-times
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   regridworkflow.py
#
$ git checkout master
M       mms/src/main/python/regridworkflow.py
Switched to branch 'master'
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   regridworkflow.py
#

...为什么分支 master 显示我在分支 regrid-more-flexible-times 上进行的更改?

我的目标是能够在分支 regrid-more-flexible-times 上进行更改,但能够签出没有这些更改的 master 分支。为什么上面的工作流程不这样做,我应该怎么做才能实现?

I've made changes in my branch regrid-more-flexible-times.

不,您已经更改了工作副本。

why does branch master show the changes I staged while on branch regrid-more-flexible-times?

每个工作副本只有一个暂存区。它们不存在于每个分支。请参阅 git stash 了解如何在不提交更改的情况下保存更改。

根据 the documentation:

git checkout <branch>

To prepare for working on , 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>.

这就是为什么当您检出主控时您未暂存的更改仍然存在的原因。


您可能想将更改提交到分支然后检出 master,或者使用 git stash 保存更改而不提交它们。