In the online version of the Pro Git book, I find the sentence "The HEAD in Git is the pointer to the current branch reference, which is in turn a pointer to the last commit you made or the last commit that was checked out into your working directory.". When reading about branching, I read that a branch is created from current HEAD, e.g. here. In my naivety, I conclude that creating a branch carries over anything that is committed (as it is part of HEAD) and nothing more (i.e. also not what is part of the index via staging). However, I find that staged changes from the parent branch are also part of the newly created child branch. Isn't that counterintuitive since my branch should contain the HEAD which in turn is the latest commit in the parent branch?
Example
A repo with one committed file "testfile" in only existing branch master
. I add a line to this file echo "test a new line" >> testfile
, stage it via git add .
. Now I decide to do something else but need a new branch for that. I create git branch testbranch
. Now I like to switch to the new branch (actually I would expect that git won't let me switch since I have staged but uncommitted changes on master
but it will) git checkout testbranch
. Now git status tells me I have changes to be committed in my testbranch
. Why? I created the testbranch to do other things and expected it to contain what was in the HEAD of its parent branch (master
).