I understand that a command to start a new branch is
git checkout -b <mybranch>
However, that assumes I had the foresight to have created the new branch before making the new changes that I want to commit.
Usually however that's not the case. Usually, I start coding, then may or may not do git add .
then want to commit in a new branch. How do I do that?
Go ahead and create the new branch, which essentially 'saves' what is on your current branch to the new branch.
Switch back to the previous branch and 'undo' your recent actions to restore that first branch to where you wanted it to be if you started the new branch at an ideal place.
(If it's only a few things - command z, or maybe go back to the prior commit, or re-pull from the last version on Github.) Then git add and commit that first branch, and you can switch back to the second to continue working.
Maybe not ideal, but I've used it as a work-around.