为什么 Git 允许切换分支并且 (!!!) 在切换后不更新工作树?

Why Git allows switch branches and (!!!) doesn't update the working tree after this switching?

Git 对于 Windows 2.5.2。我希望如果索引有未提交的更改,那么 Git 将禁用对其他分支的检查。但我看到了其他结果。示例脚本:

#!/bin/bash
# script.sh
# Run this script in the Git Bash.
git --version
host="./sandbox"
if [ -d $host ]; then
    rm -r $host
    mkdir $host
    echo \"$host\" directory recreated...   
else
    mkdir $host
    echo $host directory \"$host\" created.
fi
cd $host
git init -q
touch 1.txt 2.txt 3.txt
git add "*"
git commit -qm "Initializing"
git branch
ls
git checkout -qb branch#1
rm 3.txt
echo ABCD >> 1.txt
# git commit -aqm "branch#1_commit#1"
git branch
ls
git checkout -q master
git branch
ls
git checkout -qb branch#2
rm 2.txt
echo 1234 >> 1.txt
# git commit -aqm "branch#2_commit#1"
git branch
ls
git checkout -q master
git branch
ls

我有这样的输出:

bushm@DESKTOP-ISD2NUH MINGW64 /d/src
$ /d/src/script.sh
git version 2.5.0.windows.1
./sandbox directory "./sandbox" created.
* master
1.txt  2.txt  3.txt
* branch#1
  master
1.txt  2.txt
  branch#1
* master
1.txt  2.txt
  branch#1
* branch#2
  master
1.txt
  branch#1
  branch#2
* master
1.txt

bushm@DESKTOP-ISD2NUH MINGW64 /d/src
$

我看到切换完成了 但是切换后工作树是相同的(一个文件而不是三个文件)(未更新)。我预计在索引切换后其内容将被取消并且分支的最后一次提交将被提取到工作树中。为什么工作树没有根据 master 分支最后一次提交的内容更新?

I expect that if the index has the uncommited changes, then Git will disable the checking to other branches.

这种期望是错误的。您可以预期的是,如果切换到不同的分支可能会导致您丢失数据,那么 Git 将阻止您切换到该不同的分支(除非您强制执行)。但就您而言,切换到其他分支不会对您造成任何伤害。您对某些文件进行了本地更改,但这些文件在所有分支中都是相同的,因此可以简单地保留本地更改。