I have a branch name test1 and I make a new branch named as test2 by following command. Currently my branch is test1 I run this command
$ git checkout -b test2
It creates the new branch and work on that test2 branch. After completing my my task on test2 branch I switch back to my original branch test1 but it also merge the changing of test2 branch that is not good. Because I want to separate the logic of both branches. Now my original branch also become unstable.
I just want to know that why test2 code merge in my test1 branch when I checkout?
Uncomitted changes don't belong to any specific branch, that's why they will just stay in the working directory. When those changes would conflict with changes from the other branch,
git checkoutwill abort and say so.You have to commit your changes on branch test2. After this, when you checkout test1, those changes will disappear.
Note that people are often reluctant of committing changes because they're not yet finished with their work. In git, it's hardly a problem, because it's easy enough to amend the last commit, or undo it with
git reset HEAD^and recommit with the latest changes.