This is my first attempt to use branches and it is not behaving in a logical way to me. I created a new branch and made some changes. The unstaged changes look correct. I then switched back to the master branch. However, the changes I made still show up. I expected those changes to show in the branch and not show in the master. What am I missing about branches? TIA.
Your changes will be referenced by a ("belong to") branch only after you have committed them while the branch is checked out.
If you want to switch back to master without keeping the changes in the working directory, you can just commit them before checking out master. When you switch back later on to your branch you'll get them again in the working directory. You can then still change the content of the commit with the "amend" feature.
Another possibility is to use the stash feature. But I find it more usable just to create a preliminary commit.
Thanks for the reply. I will give that a try but it seems counter intuitive that a commit in the branch is needed to get the master to revert. Indeed, logically it seems the master should never show changes made in a branch until merged.
Although it might not be obvious at first, there's a good reason why un-staged changes remain when you switch branches. The use case goes like this:
You start working on a new task thinking that it's too small to bother making a new branch for this work. Then you start making changes and begin to realize your assumption was wrong, it really does need its own branch. Git's behavior makes this simple: just create and checkout the new branch and whatever changes you made so far are still available. I typically commit them immediately in the new branch.
This happens to me, and every developer I work with, pretty frequently. Developers are always under-estimating in this way.
The downside is what you ran into: if you have un-staged changes for some other reason and you decide to switch branches you should do something about those changes before the switch. But this is easy: if you have any reason at all to think you'll want them when you go back to the branch you're about to leave, commit them, as Stefan says; if you're certain you have no further use for them, replace them with the HEAD version.