I execute the following sequence of commands:
git init rep
cd rep/
echo '111' > 1.txt
git add 1.txt
git commit -m '1'
git checkout -b dev
echo '222' > 1.txt
git checkout master
more 1.txt
As a result of these commands I see
222
And I do not understand why. As you can see I create and go into the 'dev' branch. I do some changes there but I do not add and do not commit them. Why after going back from 'dev' to 'master' I do see the changes that I did in 'dev'? Shouldn't they stay in dev until I add, commit and merge them back to master?
git add
stages the changes into the index.git commit
takes a snapshot of all the tracked files in the index as a commit. A branch is a ref that points to a commit. In your case, the changes are still in the work tree. The branch doesn't know about them yet. – Begrudge