我显然完全不理解git。这就是我得到的:
git branch (outputs that I'm on master)
git checkout -b foo
echo "next line" >> file (file is an existing file)
git add file (stages)
git checkout master
git status (shows that file has "next line" and is staged!!)
git commit (commits the changes that were staged on branch foo!!)
git checkout foo
问题的关键在于,现在对文件所做的任何更改(包括修改文件和暂存)都会发生在所有分支上。当你提交到特定的分支时,那些更改将丢弃除了你提交到的那一个分支以外的所有其他分支。
这实际上就是正在发生的事情吗?有人能让我理解一下吗?这听起来像是完全混乱的行为,显然我不明白设计理念是什么,使这成为一个合理的事情。
以下是一个具体的例子:
$ mkdir element
$ cd element
$ git init
Initialized empty Git repository in /home/dan/element/.git/
$ echo "one" >> one
$ git add one
$ git commit -m msg
[master (root-commit) 36dc8b0] msg
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 one
$ git checkout -b fire
Switched to a new branch 'fire'
$ echo "next line" >> one
$ git checkout master
M one
Switched to branch 'master'
$ cat one
one
next line
$
这显然与《Git Pro Book》中所说的相矛盾:
这是一个需要记住的重要要点:Git 会将你的工作目录重置为检出的分支指向的提交快照的样子。它会自动添加、删除和修改文件,以确保你的工作副本是该分支上最后一次提交时的样子。