No need to stash.
Update 2020 / Git 2.23
Git 2.23 adds the new switch
subcommand, in an attempt to clear some of the confusion caused by the overloaded usage of checkout
(switching branches, restoring files, detaching HEAD, etc.)
Starting with this version of Git, replace the git checkout
command below with:
git switch -c <new-branch>
The behavior remains unchanged.
Before Update 2020 / Git 2.23
git checkout -b new_branch_name
does not touch your local changes. It just creates the branch from the current HEAD and sets the HEAD there.
So I guess that's what you want.
--- Edit to explain the result of checkout master ---
Are you confused because checkout master
does not discard your changes?
Since the changes are only local, git does not want you to lose them too easily. Upon changing branch, git does not overwrite your local changes. The result of your checkout master
is:
M testing
, which means that your working files are not clean. git did change the HEAD, but did not overwrite your local files. That is why your last status still show your local changes, although you are on master
.
If you really want to discard the local changes, you have to force the checkout with -f
.
git checkout master -f
Since your changes were never committed, you'd lose them.
Try to get back to your branch, commit your changes, then checkout the master again.
git checkout new_branch
git commit -a -m"edited"
git checkout master
git status
You should get a M
message after the first checkout, but then not anymore after the checkout master
, and git status
should show no modified files.
--- Edit to clear up confusion about working directory (local files)---
In answer to your first comment, local changes are just... well, local. Git does not save them automatically, you must tell it to save them for later.
If you make changes and do not explicitly commit or stash them, git will not version them. If you change HEAD (checkout master
), the local changes are not overwritten since unsaved.