Let say I have two branch - main and dev and I am currently in main branch
If I made some changes in certain files and save, then checkout to dev branch,
I find there still contains the changes I made in main branch.
Is it normal?
What should I do to avoid this situation?
That is normal; those changes are both uncommitted and unstashed, which is to say that they exist purely on your filesystem (outside of Git). Also note that changes you make are not tied to a particular Git branch until they are committed.
If you find yourself in this situation, the most likely reason is that you wanted to stop working on one feature and start working on another one.
commit
those changes before swapping branches (you can always swap back to the old branch, commit, then swap to the new one if you forget). This will affix the changes to the branch, though it will still only be on your local branch (and invisible to other team members) unless you push
to origin
.stash
the changes. Stashes mention they came from a particular branch, though a stash can be applied to any branch. This is useful if you accidentally started working on the wrong branch.Both approaches will give you a clean copy of the new branch.