Git-Staging und Commit zwischen mehreren Zweigen

Ich verstehe git eindeutig überhaupt nicht. Das bekomme ich:

<code>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
</code>

Hier ist der Kicker. foo zeigt jetzt keine Änderungen an, die an der Datei im Arbeitsverzeichnis vorgenommen ODER gespeichert wurden.

So sieht es aus - alle Änderungen, die Sie vornehmen, einschließlich der Änderung von Dateien und der Bereitstellung, werden ALLEN Zweigen zugewiesen. Wenn Sie sich für einen bestimmten Zweig verpflichten, werden diese Änderungen in allen anderen Zweigen außer dem, für den Sie sich verpflichtet haben, verworfen.

Ist das wirklich was los? Kann das jemand für mich sinnvoll machen? Es klingt nach total bescheuertem Verhalten und ich verstehe offensichtlich nicht die Designidee, die dies zu einer vernünftigen Sache macht.

Zum expliziten Beispiel bearbeiten:

<code>$ 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
$
</code>

Das widerspricht offenkundig dem git pro book:

This is an important point to remember: Git resets your working directory to look like the snapshot of the commit that the branch you check out points to. It adds, removes, and modifies files automatically to make sure your working copy is what the branch looked like on your last commit to it.

Antworten auf die Frage(2)

Ihre Antwort auf die Frage