不同的分支共享一个索引文件?

Different branches share a single index file?

我对下面的场景感到困惑

git init  (command 0)
Initialized empty Git repository in a path;

echo line1 >a (command 1)
git add a  (command 2)`

git commit -m 'first commit' (command 3)
1 file changed, 1 insertion(+)
create mode 100644 a

git branch b (command 4)
echo line2 >>a (command 5)

git checkout b  (command 6)
Switched to branch 'b'

git status (command 7)
Changes not staged for commit

git checkout master (command 8)
Switched to branch 'master'

git add a  (command 9)
git status (command 10)
Changes to be committed

git checkout b  (command 11)
Switched to branch 'b'

git status  (command 12)
Changes to be committed:

Q1:由于分支b是commit后创建的,所以b的初始仓库应该包含文件'a',切换到b后,工作目录应该用分支b上的最后一次提交初始化。但是工作目录中的文件a没有改变(仍然包含2行),这是某种防止丢失更改的机制吗?

Q2:在命令8之后,当前分支应该是master,file'a'然后被添加到index然后checkout回到分支b,此时,gitstatusreturns(要提交的更改)。但是我在master分支添加了文件'a',为什么我可以在另一个分支提交呢? 我对结帐分支时索引文件如何更改(或不更改)感到有些困惑

提前致谢!

意识到:将第二行添加到文件后,您还没有提交任何内容。话虽如此

好的... git 尝试保存您未提交的更改,除非您强制它丢失它们。它甚至会阻止你做一些事情,这样你就不会丢失你的更改。

在命令 5 和 6 之间,您还没有提交对文件的更改,实际上,它甚至不在索引上。命令 6 切换到 b,并且该版本正指向您所处的相同版本,因此 git 方式 "ok",移动到 b。您仍然可以获得文件 un-staged.

在命令 8 上,你将 back 切换到 master,git 说 "of course"(你没有移动到另一个版本,所以你很好) .

命令 11,文件终于进入阶段...但又没有从当前修订版移动,所以一切正常。

想看看 git 有时如何让您无法移动?立即提交(在分支 b 上)然后执行此操作:

git branch c master
git checkout c
echo hello > a
git checkout b # and here git should keep you from moving.